Wagtail Tutorial Series:
To learn more about Wagtail CMS, please check Build Blog With Wagtail CMS (4.0.0)
- Create Wagtail Project
- Modern Frontend Techs for Wagtail
- Dockerizing Wagtail App
- Add Blog Models to Wagtail
- How to write Wagtail page template
- Create Stylish Wagtail Pages with Tailwind CSS
- How to use StreamField in Wagtail
- Wagtail Routable Page
- Add Pagination Component to Wagtail
- Customize Wagtail Page URL
- Add Full Text Search to Wagtail
- Add Markdown Support to Wagtail
- Add LaTeX Support & Code Highlight In Wagtail
- How to Build Form Page in Wagtail
- How to Create and Manage Menus in Wagtail
- Wagtail SEO Guide
- Source code: https://github.com/AccordBox/wagtail-tailwind-blog
Wagtail Tips:
- Wagtail Tip #1: How to replace ParentalManyToManyField with InlinePanel
- Wagtail Tip #2: How to Export & Restore Wagtail Site
Write style in Wagtail:
- How to use SCSS/SASS in your Django project (Python Way)
- How to use SCSS/SASS in your Django project (NPM Way)
Other Wagtail Topics:
Introduction
After reading this Wagtail tip article, you will get:
- How to export your existing Wagtail site content using Django
dumpdatacommand - How to restore the site content to a new Wagtail site using Django
loaddata - 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)
- Create Wagtail Project
- Modern Frontend Techs for Wagtail
- Dockerizing Wagtail App
- Add Blog Models to Wagtail
- How to write Wagtail page template
- Create Stylish Wagtail Pages with Tailwind CSS
- How to use StreamField in Wagtail
- Wagtail Routable Page
- Add Pagination Component to Wagtail
- Customize Wagtail Page URL
- Add Full Text Search to Wagtail
- Add Markdown Support to Wagtail
- Add LaTeX Support & Code Highlight In Wagtail
- How to Build Form Page in Wagtail
- How to Create and Manage Menus in Wagtail
- Wagtail SEO Guide
- Source code: https://github.com/AccordBox/wagtail-tailwind-blog
Wagtail Tips:
- Wagtail Tip #1: How to replace ParentalManyToManyField with InlinePanel
- Wagtail Tip #2: How to Export & Restore Wagtail Site
Write style in Wagtail:
- How to use SCSS/SASS in your Django project (Python Way)
- How to use SCSS/SASS in your Django project (NPM Way)
Other Wagtail Topics: