gpt4 book ai didi

python - Django loaddata 验证错误

转载 作者:行者123 更新时间:2023-12-01 06:18:29 26 4
gpt4 key购买 nike

这个thread (对我来说)太困惑了,所以我再次问这个问题。我无法使原始问题中提到的 csv2json.py 脚本工作。我只是想找到一种将数据导入到 sqlite3 数据库的方法。

这是我正在使用的模型:

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

我将此文件放在应用程序目录中的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,
...

最佳答案

(I'll fix the duplicate "school" later.)

其实,这是你的问题。第二个将 school 定义为外键将要求它是整数,因此会出现错误。

您可以通过使用

转储表的架构来确认这一点

sqlite3 <database-file> '.schema wkw2_Lawyer'

关于python - Django loaddata 验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1894628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com