Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Action to generate a json document with all objects and related objects
# -*- 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))
[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
You can’t perform that action at this time.