Clara
Clara

Reputation: 3035

Mongoengine - id field is named None

I have noticed a weird thing happening in my application. I just can't figure out what is the systematic error that I am doing, that makes my id field to be named None. I am using mongoengine and I defined a document such as:

class TestDoc(Document):
    myField = StringField()
    secondField = StringField()

Then, when I instantiate a Python object of type TestDoc this is what happens:

doc = models.TestDoc(myField="My field")
doc.save()

print doc.id
print "TESTING DOC: ", doc.__dict__

The result to this is:

>> 516d4e3cd836195263fdd45b
>> TESTING DOC:  {'_created': False, '_data': {None: ObjectId('516d4e3cd836195263fdd45b'), 'secondField': None, 'myField': 'My field'}, '_changed_fields': [], '_initialised': True}

I don't understand why instead of a field named "id" I end up having a weird field called None, having as value the actual id. In the DB everything seems to be fine, the documents look ok, but as soon as I retrieve a doc and wish to process it or convert it to json in order to send it over the network, this makes me problems. Can anyone help?

Upvotes: 0

Views: 1216

Answers (1)

Ross
Ross

Reputation: 18111

The _data dictionary is an internal mongoengine dictionary and those values may not be the same values stored in the database.

Why None instead of _id? I'm not 100% sure, lost in the mists of time - but it was fixed in #255 and will be _id in the next release.

Upvotes: 1

Related Questions