djangodjango-modelsdjango-fixtures

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


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


Solution

  • 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.