Django loaddata ValidationError [英] Django loaddata ValidationError

查看:248
本文介绍了Django loaddata ValidationError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我使用的模型:

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.)

我有这个data1.csv文件: / p>

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

pk,然后根据脚本的需要手动输入1.

I have to put in "pk" and 1 manually as required by the script.

然后,我运行脚本,得到data1.csv.json文件:

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



我将此文件放在fixtures文件夹中app目录并运行 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.

当我用脚本注释掉脚本中的两行时,我得到这个错误信息: / p>

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'

我做错了什么?

编辑:

我取出了一年的报价使它成为一个整数,但我仍然有同样的ValidationError:

I took out the quotes around the year to make it an integer but I still got the same ValidationError:

...
            "year_graduated": 2005, 
...


推荐答案


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

(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.

您可以通过转储表的模式来确认这一点。 p>

You can confirm this by dumping the schema of your table with


sqlite3 < database-file> '.schema wkw2_Lawyer'

sqlite3 <database-file> '.schema wkw2_Lawyer'

这篇关于Django loaddata ValidationError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆