Django loaddata 验证错误

1 投票
1 回答
1777 浏览
提问于 2025-04-15 16:57

这个讨论串让我有点困惑,所以我再问一次。我没办法让原问题中提到的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, 
...

1 个回答

2

(我稍后会修复重复的“school”。)

其实,这就是你的问题。第二个“school”的定义作为外键需要是一个整数,所以才会出现这个错误。

你可以通过以下命令来确认这一点,查看你表的结构:

sqlite3 <数据库文件> '.schema wkw2_Lawyer'

撰写回答