我无法在 MongoEngine 中使用 unique_with
处理 ReferenceFields
。我的模型如下所示:
class WorkoutSchedule(database.Document):
""" Defines a workout schedule """
user = database.ReferenceField(User)
title = database.StringField(
required=True,
min_length=3,
max_length=30,
unique_with=user)
如果 user
和 title
相同,我希望文档是唯一的。但是,这会引发异常:
TypeError: Error when calling the metaclass bases
'ReferenceField' object is not iterable
是否可以使用 unique_with
和 ReferenceFields
还是我必须手动解决这个问题?
最佳答案
unique_with (Default: None)
A field name (or list of field names) that when taken together
with this field, will not have two documents in the collection
with the same value.
因此 unique_with
必须是字段名称的 basestring
或 list
的 basestring
:
class WorkoutSchedule(database.Document):
""" Defines a workout schedule """
user = database.ReferenceField(User)
title = database.StringField(
required=True,
min_length=3,
max_length=30,
unique_with=['user'])
关于python - 是否可以在 ReferenceFields 上使用 unique_with ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18132283/