Django loaddata error

2024/9/8 10:53:55

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

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"}}
]
https://en.xdnf.cn/q/72688.html

Related Q&A

Parsing JSON string/object in Python

Ive recently started working with JSON in python. Now Im passing a JSON string to Python(Django) through a post request. Now I want to parse/iterate of that data. But I cant find a elegant way to parse…

Removing NaNs in numpy arrays

I have two numpy arrays that contains NaNs:A = np.array([np.nan, 2, np.nan, 3, 4]) B = np.array([ 1 , 2, 3 , 4, np.nan])are there any smart way using numpy to remove the NaNs in b…

Run Python + OpenCV + dlib in Azure Functions

I have created an image processing script in Python (with dlib and OpenCV) - I was wondering how I can bring this functionality to Azure Functions, so that the script can be called via an API. As Pytho…

Best way to add python scripting into QT application?

I have a QT 4.6 application (C++ language) and i need to add python scripting to it on windows platform. Unfortunately, i never embed python before, and it seems to be a lot of different ways to do so.…

Unexpected behavior of python builtin str function

I am running into an issue with subtyping the str class because of the str.__call__ behavior I apparently do not understand. This is best illustrated by the simplified code below.class S(str):def __ini…

How to set cookies with GAE/Python for 1 month?

I need to implement the following:User input user id and pass We validate that on another server If they are correct, cookies with these details should be saved for one month Each time user uses my sit…

connection refused with Celery

I have a Django project on an Ubuntu EC2 node, which I have been using to set up an asynchronous using Celery. I am following How to list the queued items in celery? along with the docs, to experiment…

Routing all packets through my program?

I want to build an application that routes all network traffic (not just HTTP) through my application. Basically, what I want is all the traffic to be given to my application (they should never reach t…

python struct.pack(): pack multiple datas in a list or a tuple

Say i have a list or a tuple containing numbers of type long long,x = [12974658, 638364, 53637, 63738363]If want to struct.pack them individually, i have to use struct.pack(<Q, 12974658)or if i want…

Can I pass a list of colors for points to matplotlibs Axes.plot()?

Ive got a lot of points to plot and am noticing that plotting them individually in matplotlib takes much longer (more than 100 times longer, according to cProfile) than plotting them all at once. Howev…