ChildPage.objects.child_of(self) 和 ParentPage.get_children() 有什么区别? [英] What is the difference between ChildPage.objects.child_of(self) and ParentPage.get_children()?

查看:17
本文介绍了ChildPage.objects.child_of(self) 和 ParentPage.get_children() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为 ChildPage.objects.child_of(self)ParentPage.get_children() 产生相同的结果,因为 subpage_typesParentPage 只是一个 ['ChildPage'].

I think that ChildPage.objects.child_of(self) and ParentPage.get_children() produce same result because subpage_types of ParentPage is just one ['ChildPage'].

但是当我尝试过滤 ParentPage.get_children() 的结果时出现错误.

But when I try to filter the result of ParentPage.get_children() there is an error.

    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)

        child = self.get_children().live().public()               # <- don't works
        child = ChildPage.objects.child_of(self).live().public()  # <- works

        if request.GET.get('tag', None):
            tags = request.GET.get('tag')
            child = child.filter(tags__slug__in=[tags])  # <- error here

        context["child"] = child
        return context

回溯(最近一次调用最后一次):

Cannot resolve keyword 'tags' into field. Choices are: alias_of, alias_of_id, aliases, blogindexpage, blogpage, content_type, content_type_id, depth, draft_title, expire_at, expired, first_published_at, formsubmission, go_live_at, group_permissions, has_unpublished_changes, homepage, id, last_published_at, latest_revision_created_at, live, live_revision, live_revision_id, locale, locale_id, locked, locked_at, locked_by, locked_by_id, numchild, owner, owner_id, partnerindexpage, partnerpage, path, redirect, revisions, search_description, seo_title, show_in_menus, sites_rooted_here, slug, title, translation_key, url_path, view_restrictions, workflow_states, workflowpage

推荐答案

使用 self.get_children(),子页面的页面类型是事先不知道的——一个页面的子页面可能包含多个不同种类.由于 Django 查询集不(作为标准*)支持组合来自多个模型的数据,结果以基本的 Page 类型返回,它只包含所有页面共有的核心字段,例如标题和蛞蝓.过滤 tags 因此失败,因为页面模型中不存在该字段.

With self.get_children(), the page type of the child pages is not known in advance - a page's children may include multiple different types. Since Django querysets don't (as standard*) support combining data from multiple models, the results are returned as the basic Page type, which just contains the core fields common to all pages such as the title and slug. Filtering on tags therefore fails, because that field does not exist on the Page model.

使用 ChildPage.objects.child_of(self),Django 预先知道页面类型是 ChildPage - 如果 self 有任何其他类型的子页面 - 他们会不包含在结果中 - 因此它可以直接在 ChildPage 表上查询,因此 ChildPage 的所有字段(包括 tags)都可用于过滤.

With ChildPage.objects.child_of(self), Django knows in advance that the page type is ChildPage - if self had any child pages of other types - they would not be included in the results - so it can query directly on the ChildPage table, and consequently all fields of ChildPage (including tags) are available for filtering.

* Wagtail 确实在查询集上提供了一个 specific() 方法来拉入页面的完整数据,但这是在主数据库查询完成后作为后处理步骤实现的,所以这仍然不允许您对不属于基本页面模型的字段进行过滤.

* Wagtail does provide a specific() method on the queryset to pull in the full data of the pages, but this is implemented as a postprocessing step after the main database query is done, so this still won't allow you to filter on fields that aren't part of the base Page model.

这篇关于ChildPage.objects.child_of(self) 和 ParentPage.get_children() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆