Python 从queryset django中选择特定对象
是否有任何方法可以选择在查询集的结果中找到的特定对象,就像选择数组中的项目一样 在阵列中,您可以根据其位置选择特定项:Python 从queryset django中选择特定对象,python,django,Python,Django,是否有任何方法可以选择在查询集的结果中找到的特定对象,就像选择数组中的项目一样 在阵列中,您可以根据其位置选择特定项: arrayName = {55, 33, 34, 23} arrayName[2] result = 34 我想用queryset结果完成同样的事情 Users in database = {Steve, Chris, Jame, Cole, Casper, Courtney} Query will filter the names that start with c res
arrayName = {55, 33, 34, 23}
arrayName[2]
result = 34
我想用queryset结果完成同样的事情
Users in database = {Steve, Chris, Jame, Cole, Casper, Courtney}
Query will filter the names that start with c
result = {Chris, COle, Casper, Courtney}
得到结果后,我想从结果中选择Casper。。。
是否有一种方法可以像数组一样执行此操作
something like results[2]
更新
因此,我有一部分视图工作,将指定一个特定的记录
我唯一的另一个问题是看看是否有可能在HTML tempalte文件中执行相同的操作。。。下面是我在视图和html文件中看到的内容。有没有办法做同样的事情ht ehtml文件
view.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
{%csrf_令牌%}
{{SplitFormSet.management_form}}
{SplitFormSet%中窗体的%s}
{{form.as_p}}
{%endfor%}
税款:
提示:
我试过了,但这是个错误,因为“我”不是标签
<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>
{%csrf_令牌%}
{{SplitFormSet.management_form}}
{%i=0%}
{SplitFormSet%中窗体的%s}
{{transactions[i]}
{{form.as_p}}
{%i=i+1%}
{%endfor%}
税款:
提示:
您已经将模型的字段名替换为name,为什么不试试该代码,看看它是否有效?在Python中,
{55,33,34,23}
是一个集合,而不是列表。在您给出的示例中,arrayName[2]
将引发一个TypeError
。我不知道以前为什么不起作用。我试过了,这次成功了…兄弟,你能投票给我答案吗,因为你有特权,谢谢@Omarjandali我做得很好,但是如果你能检查一下,我还有一个问题。。它将出现在我帖子的更新部分@赎罪人
<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>
names=Model.objects.filter(name__istartswith='c')
print(names[2])