ODM (Object-document mapping) is ORM too • Helps to persist objects no matter what is under the hood • Helps to build complex queries • Knows what the data scheme is looks like • Could help to maintain the DB layer (Code to DB) • Could reflect the DB schema (DB to Code) • Could help to cache data
hard to learn syntax • DjangoORM — powerful enough, easier to learn syntax • PonyORM — not that powerful, awesome syntax • Peewee — SQL powerful, SQL inspired syntax with cookies • MongoEngine — Django like ORM for MongoDB, good for start
for field_name, field in attrs.items() : field ._name = field_name attrs['_data'] = StrictDict.fromkeys(attrs.keys()) return type(name, bases, attrs) class Model(metaclass=ModelMeta): pass class Field: def __get__(self, obj, type=None): return obj._data[self._name] def __set__(self, obj, value): obj._data[self._name] = value
attr_name, attr in attrs.items(): if not isinstance(attr, getattr(cls, attr_name)): raise TypeError() return dict(**attrs) class Validator(metaclass=ValidatorMeta): pass class FooBar(Validator): foo = str bar = int FooBar(foo='spam', bar=42) == {'bar': 42, 'foo': 'spam'}