python - Django 教程 : What is get_queryset and why "model = poll" isn't needed?

标签 python django

在Django官方教程中,介绍了“通用 View ”。

我们有:

(...)
    class IndexView(generic.ListView):
        template_name = 'polls/index.html'
        context_object_name = 'latest_poll_list'

        def get_queryset(self):
            """Return the last five published polls."""
            return Poll.objects.order_by('-pub_date')[:5]


    class DetailView(generic.DetailView):
        model = Poll
        template_name = 'polls/detail.html'
(...)

(网址:https://docs.djangoproject.com/en/1.6/intro/tutorial04/#amend-views)

它说:

Each generic view needs to know what model it will be acting upon. This is provided using the model attribute.

1/那为什么我们不像在 DetailView 类中那样在 IndexView 类中使用 model = poll 呢?

2/第二个可能相关的问题是:什么是 def get_queryset() 以及我们为什么需要它?难道我们不能做像 queryset = Poll.objects.order_by('-pub_date')[:5] 这样的事情(就像类属性一样)吗?

最佳答案

这是一个完整的答案,基于文档阅读、测试、此处的答案(感谢 Daniel Roseman 和 knbk,以及一半的猜测(如果我错了请告诉我)。

通用 View 确实需要知道在哪个模型上执行。这是因为通用 View 必须在后台执行查询以检索所需的对象(这将因 View 类型、ListViewDetailView 等而异)。 所以当方法as_view()被调用时,首先调用get_queryset()并返回queryset属性的值。如果未设置后者,则调用模型对象管理器 (poll.objects) 的 all() 方法。

注意:ListViewget_queryset() 的文档 isn'nt very clear , DetailViewseems to confirm the above behavior :

Returns the queryset that will be used to retrieve the object that this view will display. By default, get_queryset() returns the value of the queryset attribute if it is set, otherwise it constructs a QuerySet by calling the all() method on the model attribute’s default manager.

除非它说 all() 方法会在我认为 objects.get(pk=) 会被调用时调用,因为它是“DetailView"而不是 ListView

最后,正如 knbk 提到的(并在文档中确认),在没有步骤参数的情况下切片时不会评估查询集,这意味着在问题的示例中(= 在 Django 官方教程中),将 queryset = Poll. objects.order_by('-pub_date')[:5] 也可以正常工作(django 1.6 测试)。

关于python - Django 教程 : What is get_queryset and why "model = poll" isn't needed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23936228/

相关文章:

python - 如何对 Pandas 中的分类列执行 groupby 和 Mean

python - 解包值过多时出现错误(预期2)

python - xlsxwriter:有没有办法定义 "named cell"?

python - 运行连接到 Django 测试数据库的 Celery worker

python - 将具有外键关系的两个模型数据传递给一个html模板

python - Django 1.9 升级问题 "django.core.exceptions.AppRegistryNotReady: Apps aren' 尚未加载。”

django - 如何使用 Win 7 XP 模式调试 Django 站点?

python - Django 中的模板目录逻辑

django - 初始化外键下拉菜单

django - 为新手制作带有图形数据库后端的基本网站的简单方法是什么?