Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django loaddata error

Tags:

python

django

I created a "fixtures" folder in the app directory and put data1.json in there.

This is what is in the file:

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

In the command line I cd to the app directory and

django-admin.py loaddata data1.json

but I get this error

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'

What am I doing wrong?

Edit:

I fixed the json format:

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

But now I get ValidationError: This value must be an integer. Is there a way to find out from the line numbers what causes the error? Only "pk" is an integer.

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.
like image 762
Zeynel Avatar asked Dec 10 '09 23:12

Zeynel


2 Answers

it looks like you are not defining your fixtures properly. Take a look at the Django Documentation. You need to define the model that you are loading, then define the fields like this

 [
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]
like image 159
John Stallings Avatar answered Sep 18 '22 10:09

John Stallings


It looks like the problem is within the fixture you've created. The following might cause problems:

  • Changing the database schema after creating a fixture, then loading the fixture into your new schema
  • Manually fiddling with the contents of the fixture.

Did you create the fixture using manage.py dumpdata?

like image 24
harto Avatar answered Sep 21 '22 10:09

harto