Saving django test database in the instrument?

I have a production database that contains a large dataset. I would like to use some of this data to run unit tests, but it all takes a rather long period at the beginning of the testing process to create the database, which I would like to avoid.

I created a test database using the command manage.py testserver

and then deleted any data I didn't want to include in the admin interface. How do I create a data binding that is left in the default test database?

+3


source to share


1 answer


you can use dumpdata

to create json device like:

./manage.py dumpdata > fixture.json



if you want to keep the fixture from your test, just do qs serialization:

# ... import your Models
from django.core.serializers import serialize


qs1 = Model1.objects.filter(...)
qs2 = Model2.objects.filter(...)
...

fixture = serialize('json', list(qs1) + list(qs2) + list(...))
with open('fixture.json', 'w') as f:
    f.write(fixture)

      

+3


source







All Articles