Django dump data for a single model?

前端 未结 9 1982
故里飘歌
故里飘歌 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:13

    I think you had the solution in your question. You can dump an individual model like this:

    ./manage.py dumpdata myapp.my_model
    
    0 讨论(0)
  • 2020-12-04 06:20

    As of version 1.1 and greater, the Django dumpdata management command allows you to dump data from individual tables:

    ./manage.py dumpdata myapp1 myapp2.my_model
    

    You can also separate multiple apps and models on the command line. Here's the canonical definition:

    django-admin dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]
    
    0 讨论(0)
  • 2020-12-04 06:26

    To write it on specific file:

    python manage.py dumpdata app_label.ModelName app_label.ModelName2 > fixtures/specic.json
    
    0 讨论(0)
  • 2020-12-04 06:27

    If you want to dump only the specified objects of a model, you can use the --pks optional argument for the dumpdata command.

    --pks PRIMARY_KEYS Outputs only the objects specified by a comma separated list of primary keys. This is only available when dumping one model. By default, all the records of the model are output.

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

    For success I had to say it twice, and specify the model two times, like:

    ./manage.py dumpdata myapp2.my_model myapp2.my_model
    

    If I only said

    ./manage.py dumpdata myapp2 myapp2.my_model
    

    I got flooded with all the models in myapp2, despite the fact that I specified my_model.

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

    As a workaround you could make another app and copy the model but point it to the existing table with the db_table meta option. Then you could just dump the models you copied into the new app. You existing app wouldn't be affected.

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