[英]What does pk__in mean in Django?
Model.objects.filter(pk__in=[list of ids])
and和
Model.objects.filter(pk__in=[1,2,3])
How do I show this data in a template?如何在模板中显示这些数据?
def xx(request):
return HttpResponse(Model.objects.filter(pk__in=[1,2,3]))
It means, give me all objects of model Model
that either have 1
, 2
or 3
as their primary key.这意味着,给我模型Model
所有对象,它们的主键是1
、 2
或3
。
See Field lookups - in .请参阅 字段查找 - 。
You get a list of objects back you can show them in the template like every other list, using the for
template tag :你会得到一个对象列表,你可以像其他列表一样在模板中显示它们,使用for
模板标签:
{% for object in objects %}
Some value: {{ object.value }}
{% endfor %}
To learn how to create a Django application you should read the tutorial or the Django book .要了解如何创建 Django 应用程序,您应该阅读教程或Django 书籍。
A tricky way to batch select (a best practice when dealing with a lot of data).批量选择的棘手方法(处理大量数据时的最佳实践)。 it would be faster than calling get/get_or_create in a loop due to the fact that only one SQL query is sent.由于只发送一个 SQL 查询,它会比在循环中调用 get/get_or_create 更快。
Try to time this :尝试计时:
Model.objects.filter(pk__in=[list of ids])
and this :和这个 :
[Model.objects.get(pk=i) for i in ids]
Also note that running on your laptop there is minimal latency when django communicates with the database (production might be different).另请注意,当 django 与数据库通信时,在您的笔记本电脑上运行时延迟最小(生产可能会有所不同)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.