Created
April 25, 2012 15:54
-
-
Save esauro/2490831 to your computer and use it in GitHub Desktop.
Action to generate a json document with all objects and related objects
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
# -*- coding: utf-8 -*- | |
__author__ = 'esauro' | |
from django.core import serializers | |
from django.db.models.deletion import Collector | |
from django.http import HttpResponse | |
from django.db import router | |
def export_related_as_json(modeladmin, request, qs): | |
"""Serializes the selected queryset and all related objects to JSON""" | |
response = HttpResponse(mimetype="text/plain") | |
# TODO: This should calculate the database instead of using "default" | |
collector = Collector(using = router.db_for_read(modeladmin.model)) | |
collector.collect(qs) | |
serializers.serialize("json", | |
[o for m,o in collector.instances_with_model()], | |
stream = response, | |
indent = 4, | |
) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment