Python Django loaddata验证错误
(对我来说)这太让人困惑了,所以我再次问这个问题。我无法使原始问题中提到的csv2json.py脚本正常工作。我只是想找到一种将数据导入sqlite3数据库的方法 以下是我正在使用的模型:Python Django loaddata验证错误,python,django,json,csv,Python,Django,Json,Csv,(对我来说)这太让人困惑了,所以我再次问这个问题。我无法使原始问题中提到的csv2json.py脚本正常工作。我只是想找到一种将数据导入sqlite3数据库的方法 以下是我正在使用的模型: from django.db import models class School(models.Model): school = models.CharField(max_length=300) def __unicode__(self): return self.
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
(稍后我将修复重复的“学校”。)
我有这个data1.csv文件:
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
我必须按照脚本的要求手动输入“pk”和1
然后,我运行脚本,得到data1.csv.json文件:
[
{
"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"
}
}
]
我把这个文件放在app目录的fixtures文件夹中,然后运行manage.py loaddata data1.csv.json
我得到了错误
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.
当我注释掉脚本中包含pk的两行时,会收到以下错误消息:
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'
我做错了什么
编辑:
我去掉了一年中的引号,使其成为一个整数,但我仍然得到了相同的ValidationError:
...
"year_graduated": 2005,
...
(稍后我将修复重复的“学校”。)
事实上,这是你的问题。将school定义为外键的第二个定义要求它是一个整数,因此是错误的
您可以通过使用转储表的架构来确认这一点
sqlite3'.架构wkw2_'
FWW,我认为这是一个学校的翻版必须是整数(一个现有学校对象的PK)根据您的模型定义。