class Post(Document): # _id field will be added automatically as # _id = ObjectIdField(defalut=lambda: ObjectId()) title = StrField(allow_blank=False, max_length=50) body = StrField() created = DateTimeField(default=lambda: datetime.utcnow()) views = IntField(default=0) rate = FloatField(default=0.0) author = RefField(User, mongo_name='user') comments = ListField(EmbDocField('models.Comment'), default=lambda: list()) class Meta: collection = 'posts' indexes = [IndexModel([('created', DESCENDING)])] default_sort = [('created', DESCENDING)]
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 EmbDoc(EmbeddedDocument): emb_float = FloatField()
class Parent1(Root): value = FloatField() name = StrField()
class Child(Parent): value = FloatField()
class Parent2(EmbeddedDocument): value = FloatField()
(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