class Doc(Document): int_field = IntField() emb_field = EmbDocField(EmbDoc, mongo_name='emb') complex_emb_field = EmbDocField(ComplexEmbDoc, mongo_name='cmplx_emb') lst_field = ListField(EmbDocField(EmbDoc), mongo_name='lst') lst_int_field = ListField(IntField(), mongo_name='lst_int') complex_lst_emb_field = EmbDocField(ComplexListDoc, mongo_name='clef')
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 Doc(Document): emb = EmbDocField('test_fields.EmbDoc') ref = RefField('test_fields.RefDoc') lst_emb = ListField(EmbDocField('test_fields.EmbDoc')) lst_ref = ListField(RefField('test_fields.RefDoc')) lst_int = ListField(IntField()) wrong_emb = EmbDocField('xxx') wrong_ref = RefField('xxx') wrong_lst_emb = ListField(EmbDocField('xxx')) wrong_emb_doc = EmbDocField('test_fields.RefDoc') wrong_ref_doc = RefField('test_fields.EmbDoc')
class Post(Document): """Post model.""" title = StrField(required=True, allow_blank=False) views = IntField(required=True, default=0) created = DateTimeField(required=True, default=lambda: datetime.utcnow()) rate = FloatField(required=True, default=0.0) author = RefField(User, required=True) comments = ListField(EmbDocField('models.Comment'), required=True, default=lambda: list()) class Meta: """Post meta.""" collection = 'posts' indexes = [ IndexModel([('title', ASCENDING)], unique=True, name='title_index'), IndexModel([('author', ASCENDING), ('created', DESCENDING)], name='author_created_index') ]
class Doc(Document): str_field = StrField(regex=r'[abc]+') int_field = IntField(gt=0, lt=10) emb_field = EmbDocField(EmbDoc) lst_field = ListField(RefField(RefDoc), min_length=1, max_length=1)
class Child(Parent): value = IntField()
class Parent1: value = IntField()
class WrongRefDoc(Document): wrong = IntField(required=False)
class Child(Mixin, Document): value = IntField()
class EmbDoc(EmbeddedDocument): int_field = IntField(required=True)
FIELD_DEFAULT = [ (AnyField, 'xxx'), (StrField, 'xxx'), (IntField, 13), (FloatField, 1.3), (BoolField, True), (DateTimeField, dt), (ObjectIdField, ObjectId('58ce6d537e592254b67a503d')), (EmailField, '*****@*****.**'), (DecimalField, Decimal('0.005')), ] @pytest.mark.parametrize('field, expected', [ (StrField(required=False), None), (IntField(required=False), None), (FloatField(required=False), None), (BoolField(required=False), None), (DateTimeField(required=False), None), (ObjectIdField(required=False), None), (EmbDocField(EmbDoc, required=False), None), (ListField(EmbDocField(EmbDoc), required=False), None), (RefField(RefDoc, required=False), None), (EmailField(required=False), None), ]) def test_field_not_exist_get_value(field, expected): class Doc(Document): value = field assert Doc().value is expected
class EmbDoc(EmbeddedDocument): int_field = IntField(mongo_name='intf')
class Parent(EmbeddedDocument): value = IntField()
class Parent2(Root): value = IntField() slug = StrField()
class Root(Document): value = IntField()
class Parent(Document): value = IntField()