简体   繁体   English

Django 1.9 加载数据错误

[英]Django 1.9 loaddata errors

ubuntu 15.10 venv python 3.4 / django 1.9 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' json: django.core.serializers.base.DeserializationError: 安装夹具时出现问题 '/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' yaml:django.core.serializers.base.DeserializationError:安装夹具“/home/nerdbox2/django_/logbook/flights/fixtures/flight_data.yaml”时出现问题:“模型”

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.在尝试了几个 csv->model 包并且没有运气之后,我决定根据 Django Docs 使用 csv-json 和 csv-yaml 的在线转换器,但仍然没有运气。 I have ~2100 records to fill the db with.我有 ~2100 条记录来填充数据库。

yeah, I'm a noob but I really have been beating this to death for the past 3 days!是的,我是个菜鸟,但在过去的 3 天里我真的把它打败了!

Any help would be appreciated!任何帮助将不胜感激!

model, json and yaml in comment below下面评论中的模型、json 和 yaml

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.有时,应用程序中的某些模型可能会导致序列化失败。一旦您运行 dumpdata 命令,Django 会将此类模型指示为警告。 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.在这里,我们认为 auth.permission 是您需要删除的表。

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)如果您使用数据库转储来加载新数据库(在另一个 django 项目中),它可能会导致 IntegrityError(如果您在同一个数据库中加载数据,它可以正常工作)

To fix this problem, make sure to backup the database by excluding contenttypes and auth.permissions tables:要解决此问题,请确保通过排除 contenttypes 和 auth.permissions 表来备份数据库:

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

Now you can use loaddata command with a fresh database现在您可以在新数据库中使用 loaddata 命令

./manage.py loaddata db.json

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

If you have dump the database excluding permission and content type you have 3 main things to checkout:如果您已转储数据库,但不包括权限和内容类型,则您需要检查 3 个主要内容:

1# See your dump json either it is proper key and value accordingly to django serialization, for example: 1# 查看您的转储 json 是否是与 django 序列化相应的正确键和值,例如:

[

      {

      "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).确保您有这样的格式,其中包括 3 个键: pkmodelfields (其余内容都在其中)。

There's a link in that page that points you to the JSON Serialization format 102, which provides a more formal and complete specification.该页面中有一个链接指向JSON 序列化格式 102,它提供了更正式和完整的规范。

2# If you are using natural key common exceptions that I have faced is because of natural key . 2#如果您使用的是自然键,我遇到的常见异常是因为自然键

You most know about natural key你最了解的自然键

3# Same migration instance (phase) for quick guideStackoverflow 3# 相同的迁移实例(阶段)用于快速指南Stackoverflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM