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

Michael Yin

Last updated on December 11 2021

Table of Contents

Wagtail Tutorial Series:

  1. Create Wagtail Project
  2. Dockerizing Wagtail App
  3. Add Blog Models to Wagtail
  4. How to write Wagtail page template
  5. Add Bootstrap Theme to Wagtail
  6. How to use StreamField in Wagtail
  7. Wagtail Routable Page
  8. Add pagination component to Wagtail
  9. Customize Wagtail Page URL
  10. Add Full Text Search to Wagtail
  11. Add Markdown Support to Wagtail
  12. Add LaTeX Support & Code Highlight In Wagtail
  13. How to Build Form Page in Wagtail
  14. How to Create and Manage Menus in Wagtail
  15. Wagtail SEO Guide
  16. Source code: https://github.com/AccordBox/wagtail-bootstrap-blog

Wagtail Tips:

  1. Wagtail Tip #1: How to replace ParentalManyToManyField with InlinePanel
  2. Wagtail Tip #2: How to Export & Restore Wagtail Site

Write style in Wagtail:

  1. How to use SCSS/SASS in your Django project (Python Way)
  2. How to use SCSS/SASS in your Django project (NPM Way)

Other Wagtail Topics:

  1. How to make Wagtail project have good coding style
  2. How to do A/B Testing in Wagtail CMS 
  3. How to build a landing page using Wagtail CMS 
  4. How to support multi-language in Wagtail CMS 

More Wagtail articles and eBooks written by me

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).

Wagtail Tutorial Series:

  1. Create Wagtail Project
  2. Dockerizing Wagtail App
  3. Add Blog Models to Wagtail
  4. How to write Wagtail page template
  5. Add Bootstrap Theme to Wagtail
  6. How to use StreamField in Wagtail
  7. Wagtail Routable Page
  8. Add pagination component to Wagtail
  9. Customize Wagtail Page URL
  10. Add Full Text Search to Wagtail
  11. Add Markdown Support to Wagtail
  12. Add LaTeX Support & Code Highlight In Wagtail
  13. How to Build Form Page in Wagtail
  14. How to Create and Manage Menus in Wagtail
  15. Wagtail SEO Guide
  16. Source code: https://github.com/AccordBox/wagtail-bootstrap-blog

Wagtail Tips:

  1. Wagtail Tip #1: How to replace ParentalManyToManyField with InlinePanel
  2. Wagtail Tip #2: How to Export & Restore Wagtail Site

Write style in Wagtail:

  1. How to use SCSS/SASS in your Django project (Python Way)
  2. How to use SCSS/SASS in your Django project (NPM Way)

Other Wagtail Topics:

  1. How to make Wagtail project have good coding style
  2. How to do A/B Testing in Wagtail CMS 
  3. How to build a landing page using Wagtail CMS 
  4. How to support multi-language in Wagtail CMS 

More Wagtail articles and eBooks written by me


Michael Yin

Michael is a Full Stack Developer from China who loves writing code, tutorials about Django, and modern frontend tech.

He has published some ebooks on leanpub and tech course on testdriven.io.

He is also the founder of the AccordBox which provides the web development services.


Table of Contents

Django SaaS Template

It aims to save your time and money building your product, developed by Michael Yin

Learn More

This book will teach you how to build a SPA (single-page application) with React and Wagtail CMS

Read More

Subscribe

Get notified about new great Web Development Tutorial