custom _id fields Django MongoDB MongoEngine

喜欢而已 提交于 2019-12-25 02:09:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!