问题
is it possible to use custom _id fields with Django and MongoEngine? The problem is, if I try to save a string to the _id field it throws an Invalid ObjectId eror. What I want to do is using my own Id's. This never was a problem without using Django because I caught the DuplicateKeyError on creation if a given id was already existing (which was even necessary to tell the program, that this ID is already taken) Now it seems as if Django/MongoEngine won't even let me create a custom _id field :-/ Is there any way to work arround this without creating a second field for the ID and let the _id field create itself?
Greetings Codehai
回答1:
You can set the parameter primary_key=True
on a Field. This will make the target Field your _id Field.
来源:https://stackoverflow.com/questions/21370889/custom-id-fields-django-mongodb-mongoengine