简体   繁体   English

django 中的 queryset 属性和 get_queryset() 方法的区别?

[英]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.

相关问题 如何从Django中的get_queryset方法返回多个queryset对象或添加queryset结果 - How to return multiple queryset object or add queryset result from get_queryset method in Django 在Django 1.9的ListView中使用get_queryset()方法 - Using get_queryset() method in ListView, Django 1.9 Django get_queryset方法的unittest失败 - Django get_queryset method's unittest failing Django:get_queryset() 没有弹出 filter() 方法 - Django:get_queryset() is not popping up filter() method Django rest 框架,应该能够覆盖 get_queryset 而不是定义 queryset 属性吗? - Django rest framework, should be able to override get_queryset and not define queryset attribute? AssertionError at /api/movies/ 'MovieViewSet' 应该包含一个 `queryset` 属性,或者覆盖 `get_queryset()` 方法 - AssertionError at /api/movies/ 'MovieViewSet' should either include a `queryset` attribute, or override the `get_queryset()` method Django Rest Framework错误无法在未设置`.queryset`或没有`.get_queryset()`方法的视图上应用DjangoModelPermissions - django rest framework error Cannot apply DjangoModelPermissions on a view that does not set `.queryset` or have a `.get_queryset()` method 如果由QuerySet.as_manager()创建,则修改Django Manager get_queryset()方法 - Modify Django Manager get_queryset() method if created by QuerySet.as_manager() Django Dry方法获取get_queryset - Django Dry approach to get_queryset Django:get_queryset(self)中的条件表达式 - Django : Conditional expressions in get_queryset(self)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM