MongoDB 使用 string(hash) _id 字段而不是整数;那么,如何获得经典的 id 主键?每次创建类实例时增加一些变量?
class Post(Document): authors_id = ListField(IntField(required=True), required=True) content = StringField(max_length=100000, required=True) id = IntField(required=True, primary_key=True) def __init__(self): //what next?
尝试创建新用户引发异常:
mongoengine.queryset.OperationError: Tried to save duplicate unique keys (E11000 duplicate key error index: test.user.$_types_1_username_1 dup key: { : "User", : "admin" })
代码:
user = User.create_user(username='admin', email='example@mail.com',
password='pass')
user.is_superuser = True
user.save()
为什么?