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