Skip to content

Instantly share code, notes, and snippets.

@bevenky
Created September 13, 2011 13:44
Show Gist options
  • Save bevenky/1213838 to your computer and use it in GitHub Desktop.
Save bevenky/1213838 to your computer and use it in GitHub Desktop.
Export json admin action with inlines
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