从查询集djang中选择特定对象

2024-10-06 07:54:31 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有任何方法可以选择在查询集结果中找到的特定对象,例如在数组中选择项。在

在数组中,可以根据位置选择特定项:

arrayName = {55, 33, 34, 23}
arrayName[2]
result = 34

我想用queryset结果完成同样的事情

^{pr2}$

在我得到结果后,我想从结果中选择Casper。。。 有没有一种方法可以像数组一样完成。在

something like results[2]

更新

所以我让视图的一部分工作,它将指定一个特定的记录。在

我唯一的另一个问题是看是否有可能在htmltempalte文件中做同样的事情。。。这是我在视图和html文件中的内容。有没有办法做同样的事情,对ehtml文件。。。在

在视图.py在

                i=0
                    for form in formset:
                        cd = form.cleaned_data
                        currentAmount = cd['amount']
                        currentDescription = cd['description']
                        print(currentAmount)
                        print(currentDescription)
                        currentTrans = transactions[i]
                        currentTrans.amount = currentAmount
                        currentTrans.description = currentDescription
                        currentTrans.save()
                        print(currentTrans)
                        i = i + 1

html格式

<form action="." method="POST">
    {% csrf_token %}
    {{ SplitFormSet.management_form }}
    {% for form in SplitFormSet %}
      {{ form.as_p }}
    {% endfor %}
    <p>Tax: <input type="text" name="tax" value=""></p>
    <p>Tip: <input type="text" name="tip" value=""></p>
    <input type="submit" name="submit" value="submit">
  </form>

我试过了,但它给我一个错误,因为“我”不是一个标签

<form action="." method="POST">
    {% csrf_token %}
    {{ SplitFormSet.management_form }}
    {% i = 0 %}
    {% for form in SplitFormSet %}
      {{ transactions[i] }}
      {{ form.as_p }}
      {% i = i + 1 %}
    {% endfor %}
    <p>Tax: <input type="text" name="tax" value=""></p>
    <p>Tip: <input type="text" name="tip" value=""></p>
    <input type="submit" name="submit" value="submit">
  </form>

Tags: 文件textnameinform视图forinput