- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这两段代码乍一看是相同的:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'
queryset = Poll.active.order_by('-pub_date')[:5]
和
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'
def get_queryset(self):
return Poll.active.order_by('-pub_date')[:5]
它们之间有什么区别吗?如果是:
哪种方法更好?或者设置 queryset
变量比覆盖 get_queryset
方法更好?反之亦然。
最佳答案
在您的示例中,覆盖 queryset
和 get_queryset
具有相同的效果。我稍微倾向于设置 queryset
,因为它不那么冗长。
当您设置queryset
时,查询集只会在您启动服务器时创建一次。另一方面,每个请求都会调用 get_queryset
方法。
这意味着如果您想动态调整查询,get_queryset
很有用。例如,您可以返回属于当前用户的对象:
class IndexView(generic.ListView):
def get_queryset(self):
"""Returns Polls that belong to the current user"""
return Poll.active.filter(user=self.request.user).order_by('-pub_date')[:5]
另一个 get_queryset
有用的例子是当你想根据可调用的对象进行过滤时,例如,返回今天的民意调查:
class IndexView(generic.ListView):
def get_queryset(self):
"""Returns Polls that were created today"""
return Poll.active.filter(pub_date=date.today())
如果您尝试通过设置 queryset
来做同样的事情,那么 date.today()
只会在加载 View 时调用一次,并且 View 一段时间后会显示不正确的结果。
class IndexView(generic.ListView):
# don't do this!
queryset = Poll.active.filter(pub_date=date.today())
关于django - 使用 get_queryset() 方法或设置查询集变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19707237/
我收到以下错误: ImproperlyConfigured at /elearning/7447932a-6044-498a-b902-97cbdd0a4001/ DetailView is miss
我通过外键字段进行了一种过滤,具有两个搜索条件:OR 和 AND 以及多项选择。 forms.py class EmployeeSkillFilter(forms.ModelForm): sk
我有一个页面,显示将根据用户选择的月份显示每月收入。 在 HTML 中,有一个包含所有可能的输入。现在在我的 I'm returning a new variable for use in the t
这两段代码乍一看是相同的: class IndexView(generic.ListView): template_name = 'polls/index.html' context_
我最近了解到,当您特别想要执行默认 View 以外的操作时,您应该重写 get 方法: class ExampleView(generic.ListView): template_name =
因为 get_queryset() 只返回一个查询集,我需要查询集 search_store 的长度到模板文件。因此,我尝试通过 get_context_data 将值发送到模板。 我知道我可以通过
我有内置的 Django API,我有返回所有对象的端点。我希望用户为我提供关键字来过滤这个查询集。最好的方法是什么。以及如何做到这一点? 它在 get_queryset 中吗?如果是,你能帮我吗!?
我的代码是 class Leads(LoginRequiredMixin, ListView): def get_queryset(self): q = self.request.GET.get('q
我正在使用 Django 细节 View 。最初,我使用了 URL 模式 url(r'^todo/details/(?P[\d]+)', views.todoDetailView.as_view(),
模型.py: from django.contrib.auth.models import User class Location(models.Model): user = models.F
我更改了 .all 方法,因此它只会选择具有 published=True 的实例: class EventManager(models.Manager): def all(self, *ar
我无法让我的自定义管理器工作... class PublicArtigoManager(models.Manager): def get_queryset(self): ret
我正在使用 Django 2.2.10。我有一个搜索栏,它使用 searchresultsview(listview) 类中的 get_queryset(self) 将搜索结果返回到显示页面。我设置了
我正在尝试关注这个 documentation .我想以某种方式将参数传递给 get_queryset,但不知道如何传递。下面的演示不工作。 class ContextAwareNotificatio
我在 DRF 文档上读到,您可以通过覆盖 .get_queryset() 来过滤查询参数。我正在寻找最佳实践,返回什么,以防过滤器不正确以及在哪里返回错误消息。 我提到的文档是 here我在下面包含源
我有这个 ModelViewset 和一个 ModelSerializer class GenerarFacturaViewset(viewsets.ModelViewSet): serial
我正在使用自定义 mixin 将过滤器应用于使用 MultipleObjectMixin 分页的基于类的 View 的查询集。当我应用过滤器以使当前页面超出数据集范围时,显然会得到 404。我想要做的
我正在覆盖 ModelViewSet 中的 get_queryset 以支持“我”作为过滤器和多个 pk 搜索: class UserViewSet(viewsets.ModelViewSet): q
我正在尝试通过 get_queryset() 函数和使用 order_by() 来编辑 listView 中的顺序当我尝试打印生成的查询时,它会忽略 order_by 查询并仅使用我的模型类中 Met
基本上,我有一个目录 View 集。在 ListView 中,我想进行一些过滤并相应地返回。 相关的目录模型字段是: class Catalog(models.Model): name = m
我是一名优秀的程序员,十分优秀!