带有固定装置的django单元测试 - 对象匹配查询不存在

django unit testing with fixtures - object matching query does not exist

我正在尝试使用固定装置在 Django 中设置单元测试。

我可以成功加载我的灯具,但是当我尝试从中检索数据时,出现错误:

DoesNotExist: BlogIndexPage matching query does not exist.

这是我的测试代码(我使用的是 Wagtail CMS,它使用 a few additional methods 扩展了单元测试):

class BlogTests(WagtailPageTests):
    fixtures = ['demosite.json']

    def test_can_create_blog_entry(self):
        blog_index_page = BlogIndexPage.objects.get(pk=5)
        self.assertCanCreate(blog_index_page, BlogPage, {
            'title': 'Post 2',
            'date': '2017-10-11',
            'intro': 'Post 2',
            'body': '<p>Test Post</p>'
        })

这是我的灯具:

[
{
    "pk": 1,
    "model": "wagtailcore.page",
    "fields": {
        "title": "Root",
        "draft_title": "Root",
        "numchild": 1,
        "show_in_menus": false,
        "live": true,
        "seo_title": "",
        "depth": 1,
        "search_description": "",
        "content_type": [
            "wagtailcore",
            "page"
        ],
        "has_unpublished_changes": false,
        "owner": null,
        "path": "0001",
        "url_path": "/",
        "slug": "root"
    }
},
{
    "pk": 2,
    "model": "wagtailcore.page",
    "fields": {
        "title": "Home page",
        "draft_title": "Home page",
        "numchild": 5,
        "show_in_menus": true,
        "live": true,
        "seo_title": "",
        "depth": 2,
        "search_description": "",
        "content_type": [
            "home",
            "homepage"
        ],
        "has_unpublished_changes": false,
        "owner": null,
        "path": "00010002",
        "url_path": "/home-page/",
        "slug": "home-page"
    }
},
{
    "pk": 5,
    "model": "wagtailcore.page",
    "fields": {
        "title": "Blog index",
        "draft_title": "Blog index",
        "numchild": 3,
        "show_in_menus": true,
        "live": true,
        "seo_title": "",
        "depth": 3,
        "search_description": "",
        "content_type": [
            "blog",
            "blogindexpage"
        ],
        "has_unpublished_changes": false,
        "owner": null,
        "path": "000100020002",
        "url_path": "/blog/",
        "slug": "blog"
    }
},
{
    "pk": 16,
    "model": "wagtailcore.page",
    "fields": {
        "title": "Blog post",
        "draft_title": "Blog post",
        "numchild": 0,
        "show_in_menus": false,
        "live": true,
        "seo_title": "",
        "depth": 4,
        "search_description": "The origin of the genus appears to be in the general area of Eastern Siberia/Mongolia. Wagtails spread rapidly across Eurasia and dispersed to Africa in the Zanclean (Early Pliocene) where the sub-Saharan lineage was later isolated. The African Pied Wagtail (and possibly the Mekong Wagtail) diverged prior to the massive radiation of the white-bellied black-throated and most yellow-bellied forms, all of which took place during the late Piacenzian (early Late Pliocene), c. 3 mya.",
        "content_type": [
            "blog",
            "blogpage"
        ],
        "has_unpublished_changes": false,
        "owner": null,
        "path": "0001000200020001",
        "url_path": "/home-page/blog-index/blog-post/",
        "slug": "blog-post"
    }
}
]

所以基本上我只是想抓取那个博客索引页面,并测试我是否可以在它下面创建一个博客页面(博客 post)。我做错了什么?

您的灯具需要包含 "model": "blog.blogindexpage""model": "wagtailcore.page" 的记录,并具有匹配的 pk 值。这是因为 Wagtail 使用 multi-table inheritance 来表示页面:页面的数据分布在 wagtailcore_page table 中(其中包含所有页面类型共有的核心字段,例如标题)和每个页面模型的另一个 table 例如 blog_blogindexpage,包含为该特定模型定义的附加字段。在两个 table 中都没有记录,对 BlogIndexPage 的查找将 return 没有结果,导致上面的 DoesNotExist 错误。

您可以 运行 ./manage.py dumpdata --indent 4 以 JSON 格式获取开发数据库的转储,供 fixture 使用;根据您的测试需要,您可以直接使用它 (./manage.py dumpdata --indent 4 > blog/fixtures/demosite.json) 或将其用作手动编写您自己的夹具的指南。