Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Simple data dump of all data in project to a Json fixture

python manage.py dumpdata --format=json myapp > /path/to/myapp/fixtures/initial_data.json

Then in tests.py add this to include fixtures:

class ViewTests(TestCase):

    # load fixtures
    fixtures = ['data2.json']

        def setUp(self):
        # continue remainder of test code
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top