Django — Easily save your model instances with dumpdata and loaddata

So I’ve always wanted to know how to write the XML fixtures for automatically loading my project db with initial data, but I always thought I’d have to learn something so I stayed away and simply entered test data by hand.

Boy was I wrong.

I had to manually write shipping tables for several shipping categories / states and state tax rates so I wanted them in a fixture I could simply load into django anywhere else.

Turns out it is amazingly easy.

Note: I previously used format=xml in this example but I’ve since switched to json (the default) because I was getting parser errors when I tried to loaddata the xml files

python dumpdata --indent=4 > shipping_fixture.json

python dumpdata
dumps all of your apps. add your app afterwards to dump only one piece.

Notice the command simply has a long output and no options to save it to a file somewhere. That’s where you use > to specify a file to write the data to.

–indent=4 just adds indentation because the default writes all data at the beginning of the line.

python loaddata shipping_fixture.json will load your data. You can also add a default fixtures setting in to have commands like flush delete the db & repopulate with your fixture.

Great stuff.. who knew it would be so freaking easy.

19 thoughts on “Django — Easily save your model instances with dumpdata and loaddata

  1. I spent 3 hours figuring out why my fixtures wouldn’t work thanks to the use of “>>” in this article.

    Use “>” instead. It doesn’t append.

  2. Not so great if you have a million records and not enough RAM to hold them all…dumpdata will churn through your swap all day… and when you try loaddata… don’t get me started. Thank goodness for Django 1.5 which fixes the loaddata swap churn, but not so sure about dumpdata.

  3. My partner and I absolutely love your blog and find almost all of your post’s to be exactly I’m
    looking for. can you offer guest writers to write content in your case?
    I wouldn’t mind creating a post or elaborating on a number of the
    subjects you write in relation to here.
    Again, awesome web log!

  4. You really make it seem really easy together with your presentation
    however I find this matter to be really something which I
    believe I would by no means understand. It kind of feels too complex and very huge for me.
    I’m having a look forward for your subsequent put up, I’ll try
    to get the dangle of it!

  5. Taking in lots of water helps your body to get rid of impurities.
    The Garden of the Gods Spa offers a full menu of relaxing, beneficial spa services including: Massage Therapy:
    Signature Massage, Peak Performance Sports Massage.

    This is how people lose weight through a colon

  6. But, none of them have been successful to match the gameplay elements and easy user interface of
    Build a Lot 3. I personally like the simple, quaint and traditional wedding chapels along the Strip.

    There are zero-down deals out there – they just aren’t always worth doing.

  7. This is the case of Google suggest, it suggests popular search combinations while
    users type in their search query. The reputation management service tries to fix this damage, which comes
    as a result of the bad reviews or complaints
    which customers may have come across as they searched through your
    website. When this happens and the person doing the search feels
    offended by this woman’s comments then the bricks will start
    to fall and her business will start to feel the impact her
    Facebook comment actually had on her online reputation.

  8. hello there and thank you for your info – I’ve definitely picked up something new from right here.
    I did however expertise a few technical points using
    this website, as I experienced to reload the site lots of times previous to I could get it to load correctly.
    I had been wondering if your hosting is OK? Not that I am complaining, but sluggish loading instances times will very frequently affect your placement in
    google and could damage your high quality score if ads and marketing with Adwords.
    Well I’m adding this RSS to my e-mail and can look out for
    a lot more of your respective intriguing content.

    Ensure that you update this again soon.

  9. Hi, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of
    spam remarks? If so how do you stop it, any plugin or anything you can advise?
    I get so much lately it’s driving me crazy so any help is very much appreciated.

  10. Wonderful weeb site. Plenty of useful information here.
    I am sending it to some pals ans additionally sharing in delicious.
    And obviously, thanks for your sweat!

  11. The cost of a new game is high, so give it some time.
    In addition such baby games could empower parents by providing support and insights into their
    own relationships with each other. Avoid using
    auction sites like e – Bay; only do so if you have no other options.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s