2009-12-10 106 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創建燈具?