[英]Difference between queryset attribute and get_queryset() method in django?
I am learning class based views in Django.我正在 Django 中学习基于 class 的视图。 I was reading the Django documentation and read about queryset attribute and the get_queryset() method.
我正在阅读 Django 文档并阅读了有关 queryset 属性和 get_queryset() 方法的信息。 When googled them I came across this answer .
当用谷歌搜索他们时,我遇到了这个答案。
I tried to replicate the result using my code:我尝试使用我的代码复制结果:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Question.objects.order_by('-pub_date')[:2]
class IndexView2(generic.ListView):
template_name = 'polls/index2.html'
context_object_name = 'latest_question_list2'
queryset = Question.objects.all()
In answer it is mentioned that when you set queryset, the queryset is created only once, when you start your server.在回答中提到,当您设置查询集时,查询集仅在您启动服务器时创建一次。 On the other hand, the get_queryset method is called for every request.
另一方面,每个请求都会调用 get_queryset 方法。
But I was able to insert questions in database and they were available in the page index2.html without restarting, I was able to change the database and changes were reflected on the page index2.html after refreshing the page.但是我能够在数据库中插入问题,并且它们在页面 index2.html 中可用而无需重新启动,我能够更改数据库并且更改反映在页面 index2.html 刷新页面后。
I further googled and found this link .我进一步搜索并找到了这个 链接。 In the DRF website, it is mentioned that queryset will get evaluated once, and those results will be cached for all subsequent requests.
在 DRF 网站中,提到查询集将被评估一次,这些结果将被缓存以供所有后续请求使用。
Can you point where I am going wrong?你能指出我哪里出错了吗? What link I am missing?
我缺少什么链接?
A QuerySet
is evaluated once, but the default implementation of get_queryset
, will use queryset.all()
, thus each time constructing a new queryset that will force reevaluation.一个
QuerySet
被评估一次,但get_queryset
的默认实现将使用queryset.all()
,因此每次构造一个新的查询集将强制重新评估。
Indeed, the implementation of the .get_queryset(…)
method [GitHub] works with:事实上,
.get_queryset(…)
方法 [GitHub]的实现适用于:
def get_queryset(self): if self.queryset is not None: queryset = self.queryset if isinstance(queryset, QuerySet): queryset =
queryset.all() elif self.model is not None: queryset = self.model._default_manager.all() else: raise ImproperlyConfigured( "%(cls)s is missing a QuerySet. Define " "%(cls)s.model, %(cls)s.queryset, or override " "%(cls)s.get_queryset()." % { 'cls': self.__class__.__name__ } ) ordering = self.get_ordering() if ordering: if isinstance(ordering, str): ordering = (ordering,) queryset = queryset.order_by(*ordering) return queryset
THis thus means that we each time make a new "copy" of the QuerySet
that will be evaluated.因此,这意味着我们每次都会创建一个将被评估的
QuerySet
的新“副本”。 In case the queryset
is not specified, it will look for the model
attribute, and work with the _default_manager
for that model.如果未指定查询集,它将查找
queryset
属性,并为该model
使用_default_manager
。
If you specified an ordering
attribute, than that means it will also order the queryset.如果您指定了
ordering
属性,则意味着它也会对查询集进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.