class DocWithMeta(Document): _id = StrField(required=True) value = IntField(required=True) name = SynonymField(_id) class Meta: collection = 'docs' indexes = [IndexModel([('value', ASCENDING)], name='value_index')] default_query = {'value': 1} write_concern = WriteConcern(w=0)
class User(Document): _id = StrField(regex=r'[a-zA-Z0-9_]{3, 20}') is_active = BoolField(default=True) posts = ListField(RefField('models.Post'), default=lambda: list()) quote = StrField(required=False) # create a synonym field name = SynonymField(_id) class Meta: collection = 'users'
class User(Document): """User model.""" _id = StrField(required=True, allow_blank=False, max_length=10) active = BoolField(required=True, default=True) posts = ListField(RefField('models.Post'), required=True, default=lambda: list()) data = AnyField(required=False) # synonyms name = SynonymField(_id)
class User(Document): _id = StrField() role = StrField(default='user') name = SynonymField(_id) class Meta: collection = 'users' @classmethod def from_mongo(cls, data): return super(User, cls.get_class(data['role'])).from_mongo(data) @classmethod def get_class(cls, role): if role == 'user': return User if role == 'admin': return Admin if role == 'customer': return Customer
class Parent(Document): _id = StrField() name = SynonymField(_id)
class DocWithoutMeta(Document): _id = StrField(required=True) name = SynonymField(_id)
class DocWithSynonymStr(Document): _id = StrField(required=True, allow_blank=False) name = SynonymField('_id')