Django loaddata ValidationError

时间:2009-12-12 20:38:24

标签: python django json csv

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

我将此文件放在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, 
...

1 个答案:

答案 0 :(得分:2)

  

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

实际上,这是你的问题。学校作为外键的第二个定义将要求它是一个整数,因此是错误。

您可以通过使用

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

sqlite3 < database-file> '.schema wkw2_Lawyer'