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 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 Doc(Document): emb_field = EmbDocField(EmbDoc)
class WrongEmbDoc(Document): wrong_emb = EmbDocField(RefDoc)
class ComplexListDoc(EmbeddedDocument): lst_field = ListField(EmbDocField(ComplexEmbDoc))
class ComplexEmbDoc(EmbeddedDocument): emb_field = EmbDocField(EmbDoc, mongo_name='emb')
(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 @pytest.mark.parametrize('field, default', FIELD_DEFAULT) def test_field_attributes(field, default): class Doc(Document): value = field(required=False)