I've a large number of models (120+) and I would like to let users of my application export all of the data from them in XML format. I looked at django-piston, but I would like to do this with minimum code. Basically I'd like to have something like this:
GET /export/applabel/ModelName/
Would stream all instances of ModelName in applabel together with it's 开发者_如何学Gotree of related objects .
I'd like to do this without writing code for each model.
What would be the best way to do this?
The standard django dumpdata command is not flexible enough to export single models. You can use the makefixture command to do that http://github.com/ericholscher/django-test-utils/blob/master/test_utils/management/commands/makefixture.py
If I'd have to do this, as a basic starting point I'd start from something like:
from django.core.management import call_command
def export_view(request, app_label, model_slug):
# You can do some stuff here with the specified model if needed
# ct = ContentType.objects.get(app_label=app_label, model=model_slug)
# model_class = ct.model_class()
# I don't know if this is a correct specification of params
# command line example: python manage.py makefixture --format=xml --indent=4 YourModel[3] auth.User[:5]
# You'll have to test it out and tweak it
call_command("makefixture", "file.xml", '%s.%s[:]' % (app_label, model_slug), format='xml')
精彩评论