繁体   English   中英

Django的loaddata错误

[英]Django loaddata error

我在应用程序目录中创建了一个“灯具”文件夹,并将data1.json放在其中。

这是文件中的内容:

[{"firm_url": "http://www.graychase.com/kadam", "firm_name": "Gray & Chase", "first": " Karin ", "last": "Adam", "school": "Ernst Moritz Arndt University Greifswald",  "year_graduated": " 2004"} ]

在命令行中,我进入应用程序目录,然后

django-admin.py loaddata data1.json

但是我得到这个错误

Installing json fixture 'data1' from
'C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures'.
Problem installing fixture
'C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures\data1.json': Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\django\core\management\commands\loaddata.py", line 150, in handle for obj in objects:
File "C:\Python26\lib\site-packages\django\core\serializers\json.py", line 41, in Deserializer for obj in PythonDeserializer(simplejson.load(stream)):
File "C:\Python26\lib\site-packages\django\core\serializers\python.py", line 76, in Deserializer
Model = _get_model(d["model"])
KeyError: 'model'

我究竟做错了什么?

编辑:

我修复了json格式:

[
    {
        "pk": 1, 
        "model": "wkw2.Lawyer", 
        "fields": {
            "school": "The George Washington University Law School", 
            "last": "Babas", 
            "firm_url": "http://www.graychase.com/babbas", 
            "year_graduated": "2005", 
            "firm_name": "Gray & Chase", 
            "first": "Amr A"
        }
    }
]

但是现在我收到ValidationError:此值必须是整数。 有没有办法从行号中找出导致错误的原因? 只有“ pk”是整数。

Problem installing fixture 'C:\~\sw2\wkw2\fixtures\csvtest1.csv.json': Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\django\core\management\commands\loaddata.py", line 150, in handle for obj in objects:
File "C:\Python26\lib\site-packages\django\core\serializers\json.py", line 41, in  Deserializer for obj in PythonDeserializer(simplejson.load(stream)):
File "C:\Python26\lib\site-packages\django\core\serializers\python.py", line 95, in Deserializer data[field.attname] =  field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
File "C:\Python26\lib\site-packages\django\db\models\fields\__init__.py", line 356, in to_python_("This value must be an integer."))

ValidationError: This value must be an integer.

看来您没有正确定义装置。 看一下Django文档 您需要定义要加载的模型,然后定义如下字段

 [
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]

看来问题出在您创建的装置中。 以下内容可能会导致问题:

  • 创建夹具后更改数据库架构,然后将夹具加载到新架构中
  • 手动摆弄灯具的内容。

您是否使用manage.py dumpdata创建了灯具?

暂无
暂无

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

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