views:

457

answers:

2

I'm new to Django coming from Rails, and I am finding the Django fixtures (most commonly JSON from what I gather), to be somewhat awkward and unweildy, at least in comparison to the rails fixtures that I am familiar with. I liked being able to embed some ruby code, e.g <%= Time.now %>, or reference other fixtures by name when relating things with foreign keys (so as to avoid having to keep track of ids).

So the question, how do you more experience Django developers build your fixtures. Do you sit down and write the JSON/XML/YAML by hand, or are there other tools available to help similar to what rails gives me? Or is it just easiest to create some data in ./manage.py shell and dump that data to a fixture, instead of writing the fixtures by hand? Or even still, do you find yourself putting a lot of data in your setUp(self) method in your test classes. Maybe writing these things out by hand is less tedious once you have a little more experience?

+2  A: 

Django's admin site is great for quickly entering dummy data or initial data. You can then dump that to a json file (or whatever format).

http://docs.djangoproject.com/en/dev/ref/django-admin/#dumpdata-appname-appname-appname-model

django-admin.py dumpdata | pbcopy will dump all the data in json format to your clipboard.

Be careful with dumping contenttypes and auth tables as that may cause problems when loading the fixture back in to a database.

Derek Reynolds
pbcopy and pbpaste I think are mac specific. In linux it is xsel. http://whereswalden.com/2009/10/23/pbcopy-and-pbpaste-for-linux/
Bryan Ward
Didn't know that! Thanks for the tip.
Derek Reynolds
A: 

Check out django-dilla. It generates random data for your models, even images. Useful for testing without the tedium of manually entering data into admin.

Lyle