Django 1.9 loaddata errors

你离开我真会死。 提交于 2021-02-05 11:10:31

问题


ubuntu 15.10 venv python 3.4 / django 1.9

command:

python manage.py loaddata flight_data.json(yaml)

errors:

json: django.core.serializers.base.DeserializationError: Problem installing fixture '/home/nerdbox2/django_/logbook/flights/fixtures/flight_data.json': 'model'

yaml: django.core.serializers.base.DeserializationError: Problem installing fixture '/home/nerdbox2/django_/logbook/flights/fixtures/flight_data.yaml': 'model'

After trying several csv->model packages and no luck, I decided to use an online converter for both csv-json and csv-yaml per the Django Docs and still no luck. I have ~2100 records to fill the db with.

yeah, I'm a noob but I really have been beating this to death for the past 3 days!

Any help would be appreciated!

model, json and yaml in comment below


回答1:


Sometimes, certain models in an app might be causing the serialization to fail.Django will indicate such models as warnings once you run the dumpdata command. Make sure you exclude such models(or the entire app in certain cases) with the following command :

./manage.py dumpdata --exclude auth.permission > db.json

Here, we consider that auth.permission is the table you need to drop.

If you use a database dump to load the fresh database(in another django project), it can cause IntegrityError (If you loaddata in same database it works fine)

To fix this problem, make sure to backup the database by excluding contenttypes and auth.permissions tables:

./manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

Now you can use loaddata command with a fresh database

./manage.py loaddata db.json

Source: https://coderwall.com/p/mvsoyg/django-dumpdata-and-loaddata




回答2:


If you have dump the database excluding permission and content type you have 3 main things to checkout:

1# See your dump json either it is proper key and value accordingly to django serialization, for example:

[

      {

      "model": "myDev.person",

      "pk": 1,

      "fields": {

              "first_name": "anjan",

              "last_name": "thakuri"

      }

    },

     {

      "model": "myDev.person",

      "pk": 2,

      "fields": {

              "first_name": "Swikriti",

              "last_name": "Thakuri"

     }

    }

]

Make sure you have a format like this, which includes 3 keys: pk, model,and fields (rest of the things are inside it).

There’s a link in that page that points you to the JSON Serialization format 102, which provides a more formal and complete specification.

2# If you are using natural key common exceptions that I have faced is because of natural key.

You most know about natural key

3# Same migration instance (phase) for quick guide Stackoverflow



来源:https://stackoverflow.com/questions/36213769/django-1-9-loaddata-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!