Django — Insert default data on syncdb. One easy command.


Order now and save 40%! But wait! Call in the next 15 minutes and we’ll double your offer, FOR FREE!

So this is the safe way to provide initial SQL data on a syncdb, in the form a .sql file in your app:

But I find it a pain in the ass.

I use the initial_data fixture to load data on syncdb. I find it extremely easy to do and requires no setup. You could literally set this up in 3 seconds. one command sounds too good to be true.

Its not very documented so I’ll post it here. I see little references to initial_data and dumpdata here and there, but yeah:

<code>python dumpdata > initial_data.json</code>

dumpdata dumps all of your models (using your default manager) in json format. check the dumpdata help for other args like –indent=Num to make it readable.

Anyways, that single command will dump the data to initial_data.json and it will be ready to go.

The only problem with this approach is if you modify your tables to have new non nullable fields this will throw an error. But if you are done with an app and it has basic categories, by all means, python dumpdata app > initial_data.json.


6 thoughts on “Django — Insert default data on syncdb. One easy command.

  1. hehee, coongrats zchronos. The things I write in this blog are entirely the things that take me a few hours to google 😦

    I have referred back to it many times

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s