自定义_id字段Django MongoDB MongoEngine [英] custom _id fields Django MongoDB MongoEngine
问题描述
是否可以使用Django和MongoEngine的自定义_id字段?
问题是,如果我尝试将一个字符串保存到_id字段,它会抛出一个无效的ObjectId错误。我想做的是使用我自己的Id。这不是没有使用Django的问题,因为如果给定的id已经存在(甚至有必要告诉程序,该ID已经被使用),我创建了DuplicateKeyError。
现在看起来好像Django / Mongo ngine甚至不会让我创建一个自定义的_id字段: - /
有没有办法工作,而不创建ID的第二个字段,让_id字段创建自己?
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?
请问Codehai
推荐答案
您可以设置参数 primary_key = True
。这将使目标字段为_id字段。
You can set the parameter primary_key=True
on a Field. This will make the target Field your _id Field.
这篇关于自定义_id字段Django MongoDB MongoEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!