class Customer(User): role = StrField(default='customer') address = StrField() class Meta: collection = 'users' default_query = {User.role.s: 'customer'}
class DraftDoc(Doc): name = StrField(allow_blank=True) draft = BoolField(default=True) class Meta: collection = 'docs' default_query = {'draft': False}
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 Search_query(Document): user_id = StrField() engine = StrField() query = StrField() result = StrField() date = DateTimeField() def to_json(self): return { 'user_id': self.user_id, 'engine': self.engine, 'query': self.query, 'result': self.result, 'date': self.date } class Meta: collection = 'search_query'
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 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 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 RefDoc(Document): str_field = StrField(required=False)
class Doc(Document): set_choices = StrField(choices={'xxx', 'yyy'}) dict_choices = StrField(choices={'xxx': 'AAA', 'yyy': 'BBB'})
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 WrongEmbDoc(EmbeddedDocument): wrong = StrField(required=True)
class Child(Parent): name = StrField()
class Comment(EmbeddedDocument): """Comment model.""" _id = ObjectIdField(required=True, default=lambda: ObjectId()) body = StrField(required=True, allow_blank=False) author = RefField(User, required=True, mongo_name='user')
class Parent(Document): name = StrField()
class Doc(Document): _id = StrField(required=True, allow_blank=False)
class Mixin: name = StrField()
class Parent1(Root): value = FloatField() name = StrField()
class Mixin(object): name = StrField()
class Parent(Document): _id = StrField() name = SynonymField(_id)
class Doc(Document): name = StrField(allow_blank=False) draft = BoolField(default=False) class Meta: collection = 'docs'
class DocWithSynonymStr(Document): _id = StrField(required=True, allow_blank=False) name = SynonymField('_id')
class DocWithoutMeta(Document): _id = StrField(required=True) name = SynonymField(_id)
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 Child3(Parent1, Parent2): value = StrField()
class DocWrongId(Document): _id = StrField(required=False)
class Admin(User): role = StrField(default='admin') class Meta: collection = 'users' default_query = {User.role.s: 'admin'}
class Parent2(Root): value = IntField() slug = StrField()
class Child(Parent, EmbeddedDocument): name = StrField()