mheavers
mheavers

Reputation: 30158

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

I am trying to set up unit testing in django using fixtures.

I can successfully load my fixtures, but when I attempt to retrieve data from them I get the error:

DoesNotExist: BlogIndexPage matching query does not exist.

Here is my code for the test (I'm using the Wagtail CMS, which extends unittest with 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>'
        })

And this is my fixture:

[
{
    "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"
    }
}
]

So basically I just want to grab that blog index page, and test whether I can create a blogpage (blog post) underneath it. What am I doing wrong?

Upvotes: 1

Views: 870

Answers (1)

gasman
gasman

Reputation: 25227

Your fixture needs to include records for "model": "blog.blogindexpage" as well as "model": "wagtailcore.page", with matching pk values. This is because Wagtail uses multi-table inheritance to represent pages: the data for a page is split across the wagtailcore_page table (which contains the core fields common to all page types, such as title) and another table such as blog_blogindexpage for each page model, containing the additional fields defined for that specific model. Without records in both tables, a lookup on BlogIndexPage will return no results, causing the DoesNotExist error above.

You can run ./manage.py dumpdata --indent 4 to get a dump of your development database in the JSON format used by fixtures; depending on the needs of your tests, you can either use this directly (./manage.py dumpdata --indent 4 > blog/fixtures/demosite.json) or use it as a guide for writing your own fixture manually.

Upvotes: 1

Related Questions