views:

34

answers:

1

I may be loading data the wrong way.

excerpt of data.json:

{
    "pk": "1",
    "model": "myapp.Course",
    "fields":
    {
        "name": "Introduction to Web Design",
        "requiredFor": [9],
        "offeringSchool": 1,
        "pre_reqs": [],
        "offeredIn": [1, 5, 9]
    }
},

I run python manage.py loaddata -v2 data:

Installed 36 object(s) from 1 fixture(s)

Then, I go to check the above object using the Django shell:

>>> info = Course.objects.filter(id=1)
>>> info.get().pre_reqs.all()
[<Course: Intermediate Web Programming>] # WRONG! There should be no pre-reqs
>>> from django.core import serializers
>>> serializers.serialize("json", info)
'[{"pk": 1, "model": "Apollo.course", "fields": {"pre_reqs": [11], "offeredIn": [1, 5, 9], "offeringSchool": 1, "name": "Introduction to Web Design", "requiredFor": [9]}}]'

The serialized output of the model is not the same as the input that was given to loaddata. The output has a non-empty pre_req list, whereas the input's pre_reqs field is empty. What am I doing wrong?

+2  A: 

I think there is already content in your many-to-many table pre_reqs (with FK=1) (before you load your JSON data).

It seems the loader will not delete already existing tuples in many-to-many tables.

Have a look at the django.core.serializer.base.DeserializedObject class. The DeserializedObject.save method only adds new relations.

maersu
did you deleted all other jsons with myapp.Course data? Is there antoher file in the fixtures path with PK=1?
maersu