January 7, 2008
Django Fixtures and Flatpage Deployment
I am currently building a small site, fairly simple and mostly static. I have written an app that is my own take on the flatpages that are in django.contrib (a post on this later) -- I basically needed a little more flexibility for different islands of content.
In building this site and setting up the appropriate flatpages in my development, I realized that I am going to need to seed the database when I release to production or else there are going to be a lot of 404 errors. Also, since the pages are referred to in my menus, I need for the url on the flatpage model to be exact. It's not that many pages, but still, I thought, there had to be a better way that either manually hand entering all the data, troubleshooting 404s for typos in the url field, or doing any manual exporting of data at the mysql prompt.
As if by some divine revelation, I happened across the chapter in the
book talking about
admin/#loaddata-fixture-fixture). What's cool about this is by dumping the
data into a file in source control, you can revision your flatpages (or any
other data that is).
Here's what I did:
$ mkdir fixtures $ django-admin dumpdata flatpages --indent=4 > fixtures/flatpages.json
Remember, I have a local app in my project called flatpages that is my replacement for the contrib version.
I tested that this data was usable by changing my setting.py file to use a different database (a new one):
mysql> create database test_import; msyql> exit; $ django-admin loaddata fixtures/flatpages.json $ python manage.py runserver
Then fired up the browser and there was all my flatpages, perfect!