[Fixed]-How can I make a fixture out of QuerySet in django?

46👍

If dumpdata doesn’t work, you can do the same through Django Serializing data.

from django.core import serializers
data = serializers.serialize("json", SomeModel.objects.all())

and then write the data on a file.

👤Ahsan

10👍

The following steps will help in making the solution complete providing support to create a fixture of various querysets.

from django.core import serializers
from django.core.management.commands.dumpdata import sort_dependencies

app_list = {}

# Add all your querysets here. The key for the dictionary can be just a 
# unique dummy string (A safe hack after reading django code)
app_list['app1_name'] = FirstModel.objects.all()
app_list['app2_name'] = SecondModel.objects.all()

# The sort_dependencies will ensure that the models are sorted so that
# those with foreign keys are taken care. If SecondModel has a fk to FirstModel,
# then sort_dependencies will take care of the ordering in the json file so that
# FirstModel comes first in the fixture thus preventing ambiguity when reloading
data = serializers.serialize("json", sort_dependencies(app_list.items()))
f = open('output.json', 'w')
f.write(data)
f.close()

Now the output will be available in output.json file. To rebuild the models from the json file:

from django.core import serializers

for obj in serializers.deserialize('json', open('output.json').read()):
    obj.save()

EDIT: Strangely, the sort_dependencies didn’t work as expected. So I ended up using python ordereddict and decided the order myself.

import collections

app_list = collections.OrderedDict()

2👍

In case you want to save json data directly to a file, you can use:

from django.core import serializers


data = YourModel.objects.all()
with open("fixtures.json", "w") as out:
    serializers.serialize("json", data, stream=out)

-1👍

I’m not sure what you mean by “outer models relations”, maybe an example would help, but you can pass dumpdata the model you’re interested in…

manage.py dumpdata --help
Usage: ./manage.py dumpdata [options] [appname appname.ModelName ...]

and there’s the exclude switch:

-e EXCLUDE, --exclude=EXCLUDE
                    An appname or appname.ModelName to exclude (use
                    multiple --exclude to exclude multiple apps/models).
👤powlo

Leave a comment