How can I make a fixture out of QuerySet in django?

Viewed 6692

Django dumpdata command is broken because it does not support any reasonable way to narrow down the amount of data dumped. I need to create a fixture of various querysets (and I don't need to take care about dumping objects from outer models relations). Limiting the number of items for those querysets, like django-test-utils makefixture does is not sufficient. Tried to achieve this by using a proxy model with custom manager, but this approach does not work - dumpdata ommits proxy models (which is reasonable).

4 Answers

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)
Related