Django loaddata错误 [英] Django loaddata error

查看:386
本文介绍了Django loaddata错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在app目录中创建了一个fixtures文件夹,并把data1.json放在那里。



这是什么文件:

  [{firm_url:http://www.graychase.com/kadam,firm_name:Gray& Chase 第一:卡林,最后一个:亚当,学校:恩斯特莫里茨安德特大学格雷夫斯瓦尔德,年度毕业:2004}] 
pre>

在命令行中,我cd到应用程序目录和

  django-admin.py loaddata data1.json 

但是我收到这个错误

 
'C:\Users\A\Documents\Projects\Django\sw2安装json fixture'data1' \wkw2\fixtures'。
安装夹具的问题
'C:\Users\A\Documents\Projects\Django\sw2\wkw2\f2sws\data1.json':追溯(最近的呼叫最后):
文件C:\Python26\Lib\site-packages\django\core\management\commands\loaddata.py,第150行,在对象中的obj的句柄:
文件C:\Python26\lib\site- packages\django\core\serializers\json.py,第41行,在Deserializer中为PythonDeserializer中的obj(simplejson.load(流)):
文件C:\Python26\lib\site- packages\django\core\serializers\python.py,第76行,Deserializer
模型= _get_model(d [model])
KeyError:'model'

做错了?



编辑:



我修复了json格式: p>

  [
{
pk:1,
model:wkw2.Lawyer ,
字段s:{
学校:乔治华盛顿大学法学院,
last:Babas,
firm_url:http://www.graychase。 com / babbas,
year_graduated:2005,
firm_name:Gray& Chase,
first:Amr A
}
}
]

但现在我得到ValidationError:该值必须是一个整数,有没有办法从行号中找出导致错误的原因?只有pk是一个整数。

 安装夹具的问题C:\〜\sw2\wkw2\fixtures\csvtest1.csv.json':追溯(最多最近的电话最后):
文件C:\Python26\Lib\site-packages\django\core\management\commands\loaddata.py,第150行,对于obj在对象中:
文件C:\Python26\lib\site- packages\django\core\serializers\json.py,第41行,在Deserializer for Python中的PythonDeserializer(simplejson。 load(stream)):
文件C:\Python26\lib\site- packages\django\core\serializers\python.py,第95行,在解串器数据[field。 attname] = field.rel.to._meta.get_field(field.rel.field_name) .to_python(field_value)
文件C:\Python26\lib\site-packages\django\db\models\fields\__init __。py,第356行,in_python_(此值必须为整数。))

ValidationError:此值必须为整数。


解决方案

看起来你没有正确定义你的夹具。看看 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
}
}
]


I created a "fixtures" folder in the app directory and put data1.json in there.

This is what is in the file:

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

In the command line I cd to the app directory and

django-admin.py loaddata data1.json

but I get this error

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'

What am I doing wrong?

Edit:

I fixed the json format:

[
    {
        "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"
        }
    }
]

But now I get ValidationError: This value must be an integer. Is there a way to find out from the line numbers what causes the error? Only "pk" is an integer.

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.

解决方案

it looks like you are not defining your fixtures properly. Take a look at the Django Documentation. You need to define the model that you are loading, then define the fields like this

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

这篇关于Django loaddata错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆