本文介绍了g单元测试:添加子页面会将其转换为基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为Wagtail创建一些单元测试,并遇到以下问题:

Trying to create some unit tests for Wagtail and running into the following problem:

>> root = FrontPage.add_root(instance=FrontPageFactory.build())
>> root
<FrontPage: article0>

>> root.add_child(instance=ArticlePageFactory.build())
<ArticlePage: article1>

>> root.get_tree()
<PageQuerySet [<Page: article0>, <Page: article1>]>

article0从输入ArticlePage变为在页面树中输入Page。此Page对象是否是对ArticlePage的引用,并且有一种我不知道要获取它的方法,或者我在这里缺少明显的东西?

"article0" goes from being type ArticlePage to type Page in the page tree. Is this Page object a reference to the ArticlePage and there's a method I'm not aware of to fetch it, or am I missing something obvious here?

只是通过将添加的文章页面存储在单独的列表中来解决该问题,但是我真的很想了解这里发生了什么。

In the meantime I've worked around the problem by just storing the added article pages in a separate list, but I'd really like to understand what's going on here.

推荐答案

Wagtail使用表示页面:所有页面类型(例如标题和子弹,以及用于跟踪诸如在页面树中的位置之类的各种内部字段)所共有的字段是基本<$ c $的一部分所有页面类型都继承自的c> Page 模型。您在 ArticlePage 上定义的其他字段在各自的表中单独存在。检索页面实例时,它可以两种可能的形式之一存在,具体取决于您通过哪种模型检索页面实例:

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>

这是一个基本的 Page 实例,并且只能访问 page.title 之类的属性,而不能访问 ArticlePage 上定义的字段/方法。

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>

这是完整的 ArticlePage 实例,允许您可以引用 page.body 之类的东西。

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

遍历页面树的操作,例如 get_tree() get_children(),始终为您提供基本的 Page 实例。这是出于性能方面的考虑-无法预先知道要返回哪种页面类型,因此无法分辨要查询哪些表以检索完整页面数据。

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.

您可以通过访问 specific Page 实例进入更具体的页面模型实例。 / code>属性-这将导致一个额外的数据库查询:

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>

您也可以调用 specific() PageQuerySet 上的方法,它将对该查询集中存在的每种不同的页面类型执行一个额外的查询:

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>]>

这篇关于g单元测试:添加子页面会将其转换为基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:55