Django dump data for a single model?

前端 未结 9 1983
故里飘歌
故里飘歌 2020-12-04 05:42

Can I perform a dumpdata in Django on just a single model, rather than the whole app, and if so, how?

For an app it would be:

python man         


        
相关标签:
9条回答
  • 2020-12-04 06:35

    As noted, you can't do this through a manage.py command in Django 1.0. However you could use a script to export the JSON file, and load it using loaddata:

    from django.core import serializers
    from myproject.myapp import models
    data = serializers.serialize("json", models.MyModel.objects.all())
    out = open("mymodel.json", "w")
    out.write(data)
    out.close()
    
    0 讨论(0)
  • 2020-12-04 06:38

    I've created a management command the generate a fixture on a per model basis. Fixtures can be generated by running:

    ./manage generate_fixtures app.model.MyModel --file=dump/MyModel.json
    

    code at: https://gist.github.com/2394883

    0 讨论(0)
  • 2020-12-04 06:39

    Take all data into json format from django model.

    Syntax:

    python manage.py dumpdata app_name.model_name
    

    For example dumping data from group_permission model which reside in default auth app in django.

    python manage.py dumpdata auth.group_permission
    

    For output take a look on console.

    0 讨论(0)
提交回复
热议问题