代码之家  ›  专栏  ›  技术社区  ›  Vasil

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

  •  0
  • Vasil  · 技术社区  · 15 年前

    我有大量的模型(120多个),我想让我的应用程序的用户以XML格式从中导出所有数据。 我看了一下django活塞,但我想用最少的代码来做这个。基本上我想要这样的东西:

    GET /export/applabel/ModelName/
    

    将applabel中modelname的所有实例与相关对象的树一起流式处理。

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

    最好的方法是什么?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Botond Béres    15 年前

    标准Django 转储数据 命令不够灵活,无法导出单个模型。您可以使用 制造夹具 命令这样做 http://github.com/ericholscher/django-test-utils/blob/master/test_utils/management/commands/makefixture.py

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

    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')
    
    推荐文章