是否可以在ReferenceFields上使用unique_with?

时间:2013-08-08 17:26:52

标签: python mongodb mongoengine

我无法让unique_with在MongoEngine中使用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)

如果usertitle相等,我希望文档是唯一的。但是,这引发了一个例外:

TypeError: Error when calling the metaclass bases
    'ReferenceField' object is not iterable

是否可以使用unique_withReferenceFields,还是必须手动解决此问题?

1 个答案:

答案 0 :(得分:1)

请参阅文档https://mongoengine-odm.readthedocs.org/en/latest/guide/defining-documents.html?highlight=unique_with#field-arguments

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必须是basestringlist个{1}}字段名称:

basestring
相关问题