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

Table of Contents

Wagtail Tutorial Series:

To learn more about Wagtail CMS, please check Build Blog With Wagtail CMS (4.0.0)

  1. Create Wagtail Project
  2. Modern Frontend Techs for Wagtail
  3. Dockerizing Wagtail App
  4. Add Blog Models to Wagtail
  5. How to write Wagtail page template
  6. Create Stylish Wagtail Pages with Tailwind CSS
  7. How to use StreamField in Wagtail
  8. Wagtail Routable Page
  9. Add Pagination Component to Wagtail
  10. Customize Wagtail Page URL
  11. Add Full Text Search to Wagtail
  12. Add Markdown Support to Wagtail
  13. Add LaTeX Support & Code Highlight In Wagtail
  14. How to Build Form Page in Wagtail
  15. How to Create and Manage Menus in Wagtail
  16. Wagtail SEO Guide
  17. Source code: https://github.com/AccordBox/wagtail-tailwind-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:

To learn more about Wagtail CMS, please check Build Blog With Wagtail CMS (4.0.0)

  1. Create Wagtail Project
  2. Modern Frontend Techs for Wagtail
  3. Dockerizing Wagtail App
  4. Add Blog Models to Wagtail
  5. How to write Wagtail page template
  6. Create Stylish Wagtail Pages with Tailwind CSS
  7. How to use StreamField in Wagtail
  8. Wagtail Routable Page
  9. Add Pagination Component to Wagtail
  10. Customize Wagtail Page URL
  11. Add Full Text Search to Wagtail
  12. Add Markdown Support to Wagtail
  13. Add LaTeX Support & Code Highlight In Wagtail
  14. How to Build Form Page in Wagtail
  15. How to Create and Manage Menus in Wagtail
  16. Wagtail SEO Guide
  17. Source code: https://github.com/AccordBox/wagtail-tailwind-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

Launch Products Faster with Django

Unlock the power of Django combined with Hotwire through SaaS Hammer. Supercharge productivity, tap into Python's rich ecosystem, and focus on perfecting your product!

Michael Yin

Michael Yin

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

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