Wagtail Tip #2: How to Export & Restore Wagtail Site

Last updated on Apr 14 2019 by Michael Yin

Introduction

After reading this Wagtail tip article, you will get:

  1. How to export your existing Wagtail site content using Django dumpdata command

  2. How to restore the site content to a new Wagtail site using Django loaddata

  3. You can know how to create test fixtures to let you write unit test for your Wagtail site.

Step 1: Learn basic concepts

Wagtail use Django ContentType model to enable “generic” relationships in db.

What you should know is that even you use same code to run migrate command, the pk of the same model in ContentType table might be different. So pk from the old site can not point to the right ContentType and this would cause loaddata fail in some cases.

We need to convert the pk to app.model and we can use natural-foreign when dumpdata.

Step2: Skip unnecessary models

Some models such as session, permission models data is not needed in some cases, so you can use -e to exclude them when dumpdata.

Step3: Dumpdata

Below is a sample dumpdata command, it is a guide and please modify if needed.

./manage.py dumpdata --natural-foreign --indent 2 \
    -e contenttypes -e auth.permission -e postgres_search.indexentry \
    -e wagtailcore.groupcollectionpermission \
    -e wagtailcore.grouppagepermission -e wagtailimages.rendition \
    -e sessions > data.json

Now you can move the data.json to root directory of new site

./manage.py migrate
./manage.py loaddata data.json

You can also exclude -e wagtailcore.pagerevision to make your data.json clean (it would only contains the latest version and very useful if you use it in unit test), but you need to edit data.json after dumpdata

"latest_revision_created_at": null,
"live_revision": null

So loaddata would not raise error.

Step4: How to troubleshoot

When the migration fail, you need to figure out what field causes the error from the output, sometimes you can try to edit the data.json and then test.

For example, if you are using PostgreSQL database and I use code snippets below to help me to do quick test.

dropdb {dbname}
create {dbname}
./manage.py migrate
./manage.py loaddata data.json

Conclusion

In this Wagtail tip, I showed you how to export & restore wagtail site using Django dumpdata & loaddata command

This method can work between two sites which have the same db schemas (same models).

If you still have problem with your project, you can also send me message and see if I can help you.

Thanks.

I'm a passionate Python developer from China and love writing code, articles about Django and Wagtail CMS.

Michael Yin

Founder of AccordBox

Need any Help in your Project?

Sign up to our newsletter