2009-12-10 87 views
2

我在应用程序目录中创建了一个“fixtures”文件夹,并将data1.json放在那里。Django加载数据错误

这是什么文件:

[{"firm_url": "http://www.graychase.com/kadam", "firm_name": "Gray & Chase", "first": " Karin ", "last": "Adam", "school": "Ernst Moritz Arndt University Greifswald", "year_graduated": " 2004"} ] 

在命令行中我cd到应用程序目录和

django-admin.py loaddata data1.json 

,但我得到这个错误

Installing json fixture 'data1' from 
'C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures'. 
Problem installing fixture 
'C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures\data1.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 76, in Deserializer 
Model = _get_model(d["model"]) 
KeyError: 'model' 

什么我做错了吗?

编辑:

我固定的JSON格式:

[ 
    { 
     "pk": 1, 
     "model": "wkw2.Lawyer", 
     "fields": { 
      "school": "The George Washington University Law School", 
      "last": "Babas", 
      "firm_url": "http://www.graychase.com/babbas", 
      "year_graduated": "2005", 
      "firm_name": "Gray & Chase", 
      "first": "Amr A" 
     } 
    } 
] 

但现在我得到ValidationError:此值必须是整数。有没有办法从行号中找出导致错误的原因?只有“pk”是一个整数。

Problem installing fixture 'C:\~\sw2\wkw2\fixtures\csvtest1.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_field(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. 
+0

您可以发布data1.json吗? – 2009-12-10 23:29:06

+1

提示:使用--indent选项dumpdata使其更具可读性。 http://docs.djangoproject.com/en/dev/ref/django-admin/#djadminopt---indent – 2009-12-11 01:09:07

回答

6

它看起来像你没有正确定义你的灯具。看看Django Documentation。您需要定义您正在加载的模型,然后定义像这样的字段

[ 
    { 
    "model": "myapp.person", 
    "pk": 1, 
    "fields": { 
     "first_name": "John", 
     "last_name": "Lennon" 
    } 
    }, 
    { 
    "model": "myapp.person", 
    "pk": 2, 
    "fields": { 
     "first_name": "Paul", 
     "last_name": "McCartney" 
    } 
    } 
] 
+0

是的,我固定夹具,现在看起来像这样,但我得到一个ValidationError:“这个值必须是一个整数“见我的编辑。我想知道是否有办法将这些数据复制并粘贴到我的Sqlite3数据库中,因为它是一个文件? – Zeynel 2009-12-12 19:28:16

1

看起来问题在于您创建的夹具内。以下可能导致问题:

  • 创建夹具后更改数据库架构,然后装入灯具到新的架构
  • 与灯具的内容手动摆弄。

您是否使用manage.py dumpdata创建灯具?