Django loaddata ValidationError [英] Django loaddata ValidationError
问题描述
这个线程对我来说太混乱了,所以我再次问这个问题。我无法使原始问题中提到的csv2json.py脚本工作。我只是试图找到一种将数据导入到sqlite3数据库的方法。
这是我正在使用的模型:
$ d
class School(Model.Model)
school = models.CharField(max_length = 300)
def __unicode __(self):
return self.school
class Lawyer(models.Model):
firm_url = models.URLField('Bio',max_length = 200)
firm_name = models.CharField('Firm',max_length = 100)
first = models.CharField('First Name',max_length = 50)
last = models.CharField('Last Name ',max_length = 50)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length = 300)
school = models.ForeignKey(School)
class Meta:
order =('?',)
def __unicode __(self):
return self.first
(稍后我会修复重复的学校) / p>
我有这个data1.csv文件:
pk,firm_url, firm_name,first,last,school,year_graduated
1,http://www.graychase.com/babbas,Gray& Chase,Amr A,Babbas,乔治华盛顿大学法学院,2005
我必须把 pk,然后根据脚本的要求手动执行。
然后,我运行脚本,并获取data1.csv.json文件:
[
{
pk:1,
model:wkw2.Lawyer,
fields:{
school:乔治华盛顿大学法学院,
last:Babbas,
firm_url:http:// www。 bb $ byear_graduated:2005,
firm_name:Gray& Chase,
first:Amr A
}
}
]
我把这个文件放在应用程序目录并运行 manage.py loaddata data1.csv.json
我收到错误
从C:\〜\Django\s安装json fixture'data1.csv' w2\wkw2\fixtures’。
安装夹具的问题C:\〜\Django\sw2\wkw2\fixtures\data1.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中为PythonDeserializer中的obj(simplejson.load(stream)) :
文件C:\Python26\lib\site- packages\django\core\serializers\python.py,第95行,在解串器数据[field.attname] =字段中。 rel.to._meta.get_fie(field.rel.field_name).to_python(field_value)
文件C:\Python26\lib\site-packages\django\db\models\fields \__init __。py,第356行,in_python _(此值必须是整数。))
ValidationError:此值必须为整数。
当我在pk中注释掉脚本中的两行时,我收到以下错误消息: / p>
从C:\〜\Django\sw2\wkw2\fixtures安装json fixture'data1.csv' 。
安装夹具的问题C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures\data1.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中为PythonDeserializer中的obj(simplejson.load(stream) ):
文件C:\Python26\lib\site- packages\django\core\serializers\python.py,第77行,Deserializer data = {Model._meta.pk .attname:Model._meta.pk.to_pytho([pk])}
KeyError:'pk'
我做错了什么?
编辑:
我拿出了一年的报价,使其成为一个整数,但我仍然有相同的ValidationError:
...
year_graduated:2005,
...
(我稍后会修复重复的学校)。
其实这是你的问题。作为外键的学校的第二个定义将要求它是一个整数,因此是错误的。
您可以通过将表的模式转储为
来确认, p>
sqlite3 < database-file> '.schema wkw2_Lawyer'
This thread got too confusing (for me) so I am asking the question again. I could not make the csv2json.py script mentioned in the original question work. I am just trying to find a way to import data to sqlite3 database.
Here's the model I am working with:
from django.db import models
class School(models.Model):
school = models.CharField(max_length=300)
def __unicode__(self):
return self.school
class Lawyer(models.Model):
firm_url = models.URLField('Bio', max_length=200)
firm_name = models.CharField('Firm', max_length=100)
first = models.CharField('First Name', max_length=50)
last = models.CharField('Last Name', max_length=50)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=300)
school = models.ForeignKey(School)
class Meta:
ordering = ('?',)
def __unicode__(self):
return self.first
(I'll fix the duplicate "school" later.)
I have this data1.csv file:
pk,firm_url,firm_name,first,last,school,year_graduated
1,http://www.graychase.com/babbas, Gray & Chase, Amr A, Babbas, The George Washington University Law School, 2005
I have to put in "pk" and 1 manually as required by the script.
Then, I run the script and I get the data1.csv.json file:
[
{
"pk": 1,
"model": "wkw2.Lawyer",
"fields": {
"school": "The George Washington University Law School",
"last": "Babbas",
"firm_url": "http://www.graychase.com/babbas",
"year_graduated": "2005",
"firm_name": "Gray & Chase",
"first": "Amr A"
}
}
]
I put this file in the fixtures folder in the app directory and run manage.py loaddata data1.csv.json
and I get the error
Installing json fixture 'data1.csv' from 'C:\~\Django\sw2\wkw2\fixtures'.
Problem installing fixture 'C:\~\Django\sw2\wkw2\fixtures\data1.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_fie(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.
When I comment out the two lines in the script with pk in them I get this error message:
Installing json fixture 'data1.csv' from 'C:\~\Django\sw2\wkw2\fixtures'.
Problem installing fixture 'C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures\data1.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 77, in Deserializer data = {Model._meta.pk.attname : Model._meta.pk.to_pytho(["pk"])}
KeyError: 'pk'
What am I doing wrong?
Edit:
I took out the quotes around the year to make it an integer but I still got the same ValidationError:
...
"year_graduated": 2005,
...
(I'll fix the duplicate "school" later.)
Actually, that is your problem. The second definition of school as a foreign key will require it to be an integer, thus the error.
You can confirm this by dumping the schema of your table with
sqlite3 <database-file> '.schema wkw2_Lawyer'
这篇关于Django loaddata ValidationError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!