Django 在大量具有子关系的模型上启用通过HTTP导出到XML

Django 在大量具有子关系的模型上启用通过HTTP导出到XML,django,django-models,Django,Django Models,我有大量的120+模型,我想让我的应用程序的用户以XML格式导出其中的所有数据。 我看了django活塞,但我想用最少的代码来完成。基本上我想要这样的东西: GET /export/applabel/ModelName/ 将applabel中ModelName的所有实例及其相关对象树一起流化 我不想为每个模型编写代码 最好的方法是什么?标准的django dumpdata命令不够灵活,无法导出单个模型。您可以使用makefixture命令来执行此操作 如果我必须这样做,作为一个基本的出发点,我

我有大量的120+模型,我想让我的应用程序的用户以XML格式导出其中的所有数据。 我看了django活塞,但我想用最少的代码来完成。基本上我想要这样的东西:

GET /export/applabel/ModelName/
将applabel中ModelName的所有实例及其相关对象树一起流化

我不想为每个模型编写代码

最好的方法是什么?

标准的django dumpdata命令不够灵活,无法导出单个模型。您可以使用makefixture命令来执行此操作

如果我必须这样做,作为一个基本的出发点,我会从以下几点开始:

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')