1

我无法unique_withReferenceFieldsMongoEngine 中使用。我的模型如下所示:

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或者我必须手动解决这个问题?

4

1 回答 1

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.

So unique_withmust be basestringor listofbasestring字段名称:

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'])
于 2013-08-09T05:54:55.317 回答