1

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

Community
  • 1
  • 1
nitzanwe
  • 412
  • 5
  • 17
  • What did you try, what error messages do you get, etc? Give more specific context, please, that way we might be able to help you. – favoretti Dec 16 '13 at 18:07
  • @favoretti i've edit my post. Tnx for your time – nitzanwe Dec 16 '13 at 18:23
  • One thing I see, that differs from JSON initial_data files that django should swallow according to their own documentation (https://docs.djangoproject.com/en/1.5/howto/initial-data/) is that your JSON object is not a list. – favoretti Dec 16 '13 at 18:25
  • it is a list, I have "[" and "]" at the end of the file. I just haven't copied it. – nitzanwe Dec 16 '13 at 18:27
  • And if you start simple and provide just one or two entries in your initial_data, would it work then? Just trying to see whether it's a deserialization error of a particular entry or just in general. – favoretti Dec 16 '13 at 18:28
  • I tried putting just two, 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)". – nitzanwe Dec 16 '13 at 18:43

1 Answers1

2

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*
Nicolas Cortot
  • 6,591
  • 34
  • 44