class Customer(User): role = StrField(default='customer') address = StrField() class Meta: collection = 'users' default_query = {User.role.s: 'customer'}
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 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 DraftDoc(Doc): name = StrField(allow_blank=True) draft = BoolField(default=True) class Meta: collection = 'docs' default_query = {'draft': False}
class Post(ValidatingDocument): _id = ObjectIdField(required=True, default=lambda: ObjectId()) user = RefField(User, required=True) title = StrField(allow_blank=False, required=True) body = StrField(allow_blank=False, required=True) created = DateTimeField(required=True) updated = DateTimeField(required=True) class Meta: collection = 'posts' async def pre_save(self): timestamp = datetime.utcnow() if not self.created: self.created = timestamp self.updated = timestamp
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() 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 User(ValidatingDocument): _id = ObjectIdField(required=True, default=lambda: ObjectId()) primary_email = StrField(allow_blank=False, required=True) password = StrField(allow_blank=False, required=True, before_set=encrypt_password) secondary_emails = ListField(StrField(allow_blank=False), required=False) given_names = ListField(StrField(allow_blank=False), required=False) family_name = StrField(required=False) nickname = StrField(required=False, allow_blank=False) created = DateTimeField(required=True) updated = DateTimeField(required=True) class Meta: collection = 'users' indexes = [ IndexModel([('primary_email', ASCENDING)], unique=True) ] def is_valid_password(self, password): return bcrypt.checkpw(password.encode(), self.password.encode()) async def pre_save(self): timestamp = datetime.utcnow() if not self.created: self.created = timestamp self.updated = timestamp
class Permission(ValidatingDocument): _id = ObjectIdField(required=True, default=lambda: ObjectId()) user = RefField(User, required=True) roles = ListField(StrField(allow_blank=False, required=True)) created = DateTimeField(required=True) updated = DateTimeField(required=True) class Meta: collection = 'permissions' indexes = [ IndexModel([('user', ASCENDING)], unique=True) ] async def pre_save(self): timestamp = datetime.utcnow() if not self.created: self.created = timestamp self.updated = timestamp
class Comment(ValidatingDocument): _id = ObjectIdField(required=True, default=lambda: ObjectId()) user = RefField(User, required=True) post = RefField(User, required=True) in_response_to = ObjectIdField() content = StrField(allow_blank=False, required=True) created = DateTimeField(required=True) updated = DateTimeField(required=True) class Meta: collection = 'comments' async def pre_save(self): timestamp = datetime.utcnow() if not self.created: self.created = timestamp self.updated = timestamp
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 Parent1(Root): value = FloatField() name = StrField()
class Mixin: name = StrField()
class Mixin(object): name = StrField()
class Comment(EmbeddedDocument): _id = ObjectIdField(default=lambda: ObjectId()) author = RefField(User) body = StrField()
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 DocWrongId(Document): _id = StrField(required=False)
class Child(Parent, EmbeddedDocument): name = StrField()
class Child(Parent): name = StrField()
class Parent(Document): _id = StrField() name = SynonymField(_id)
class Parent(Document): name = StrField()
class Parent2(Root): value = IntField() slug = StrField()
class Admin(User): role = StrField(default='admin') class Meta: collection = 'users' default_query = {User.role.s: 'admin'}
class DocWithSynonymStr(Document): _id = StrField(required=True, allow_blank=False) name = SynonymField('_id')
class Doc(Document): _id = StrField(required=True, allow_blank=False)
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 Doc(Document): name = StrField(allow_blank=False) draft = BoolField(default=False) class Meta: collection = 'docs'
class Child3(Parent1, Parent2): value = StrField()
class DocWithoutMeta(Document): _id = StrField(required=True) name = SynonymField(_id)