Created
September 13, 2011 13:44
-
-
Save bevenky/1213838 to your computer and use it in GitHub Desktop.
Export json admin action with inlines
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
def export_related_as_json(modeladmin, request, queryset): | |
opts = modeladmin.model._meta | |
for parent in queryset: | |
for inline in modeladmin.inlines: | |
related_params = inline.model.objects.filter(**{inline.fk_name: parent}) | |
from itertools import chain | |
result_list = list(chain(queryset, related_params)) | |
data = serializers.serialize("json", result_list, indent=4) | |
response = HttpResponse(data, mimetype="text/javascript") | |
response['Content-Disposition'] = 'attachment; filename=%s.json' \ | |
% unicode(opts).replace('.', '_') | |
return response | |
export_related_as_json.short_description = "Export selected with related parameters as JSON" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment