Search code examples
djangomongodbpython-2.7django-mongodb-engine

initial_data for django running with MongoDB


After much hardship, I have managed to convert my django project, that previously ran with sqlite, to run with MongoDB. This is great, beside from the fact that my old version had a massive initial_data.json file, which now fails to work with the new db when running django's syncdb command.

EDIT:

this is an example of the initial_data.json file :

    [{"pk":1, 
"model": "vcb.dishtype", 
"fields": {
    "photo": "images/dishes/breakfast.jpg", 
    "name": "Breakfast"
    }
}, 
{"pk":2, 
"model": "vcb.dishtype", 
"fields": {
    "photo": "images/dishes/bread_and_pastry.jpg", 
    "name": "Bread and pastry"
    }
    }]

and after running the syncdb I get:

DeserializationError: Problem installing fixture 'C:\Users..\initial_data.json' : u'pk'

It seems to be a problem with the MongoDB objectId and how I defined the initial_data file. I tried to remove all the pks fields from the file, but still the same error.

EDIT

I tried putting just two fixtures, if I don't set the pk, I get the same error as above. If I do set it, I get :

"DatabaseErroe: Problem installing fixture 'C:..\initial_data.json': could not load vcb.dishtype(pk=1): AutoField (default primary key) values must be strings representing an ObjectId on MongoDB (got u'1' instead)".

which is a similar problem I had with the django Site, that was solved with the help of this thread: Django MongoDB Engine error when running tellsiteid

This raises my suspicion that there's a different way to set the fixtures in this infrastructure. Maybe syncdb isn't the way to go, and there should be a sort of dump maybe?

I've searched google, and nothing seems to tackle this problem. I'm obviously asking the wrong questions.

what should I do, to create fixtures in my altered project?

thanks a lot, Nitzan


Solution

  • From your error message, I assume you are using Django MongoDB Engine?

    Your pk values must be valid ObjectIds, try using values like:

    • '000000000000000000000001'
    • '000000000000000000000002'
    • etc

    You can get ObjectIds or check that you have correct values:

    >>> from bson.objectid import ObjectId
    >>> ObjectId()
      > ObjectId('52af59bac38f8420734d064d')
    >>> ObjectId('000000000000000000000001')
      > ObjectId('000000000000000000000001')
    >>> ObjectId('bad')
    *error*