Wagtail unit testing: Adding child pages converts them to base type

≯℡__Kan透↙ 提交于 2019-12-02 09:40:59

Wagtail uses multi-table inheritance to represent pages: the fields that are common to all page types (such as title and slug, along with various internal ones used for things like tracking position within the page tree) are part of the base Page model that all page types inherit from. The additional fields you define on ArticlePage exist separately in their own table. When you retrieve a page instance, it can exist in one of two possible forms depending on which model you retrieve it through:

>> page = Page.objects.get(title='article1')
<Page: article1>

This is a basic Page instance, and only has access to properties like page.title, not fields/methods defined on ArticlePage.

>> page = ArticlePage.objects.get(title='article1')
<ArticlePage: article1>

This is a full ArticlePage instance, allowing you to refer to things like page.body.

Operations that traverse the page tree, such as get_tree() or get_children(), always give you basic Page instances. This is for performance reasons - there's no way to know in advance which page types you're going to get back, so it can't tell which tables to query in order to retrieve the full page data.

You can go from an individual Page instance into an instance of the more specific page model, by accessing the specific property - this will incur one extra database query:

>> page = Page.objects.get(title='article1')
>> page.specific
<ArticlePage: article1>

You can also call the specific() method on a PageQuerySet, which will perform one extra query for each distinct page type that exists in that queryset:

>> root.get_tree().specific()
<PageQuerySet [<FrontPage: article0>, <ArticlePage: article1>]>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!