Last active
March 3, 2024 14:22
-
-
Save czpython/b94c346e4b6cac473bff to your computer and use it in GitHub Desktop.
create fixture of django filer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core import serializers | |
from django.db.models import get_app, get_models | |
from django.db.models.query import QuerySet | |
def export_filer_models(output_file=None): | |
""" | |
Exports filer models to output_file. | |
""" | |
app = get_app('filer') | |
model_list = get_models(app) | |
# We handle filer differently because django-polymorphic interferes when serializing. | |
def get_objects(): | |
for model in model_list: | |
# Hack because of django-polyporphic D:< | |
model.objects.queryset_class = QuerySet | |
for obj in model.objects.iterator(): | |
yield obj | |
serializers.serialize('json', get_objects(), indent=2, use_natural_keys=True, stream=output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@czpython Cheers for this, I don't know how i found this, but it solved an issue I was facing with dumpdata and django-filer.