fix #32
This commit is contained in:
parent
325c735f4e
commit
2d6f368ff1
9 changed files with 63 additions and 34 deletions
|
@ -1,12 +1,14 @@
|
|||
import json
|
||||
from sqlalchemy import not_
|
||||
from sqlalchemy import not_, and_
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from datetime import datetime
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from realms import db
|
||||
from .hook import HookModelMeta, HookMixin
|
||||
|
||||
db = SQLAlchemy()
|
||||
Base = declarative_base(metaclass=HookModelMeta, cls=HookMixin)
|
||||
|
||||
|
||||
class Model(db.Model):
|
||||
class Model(Base):
|
||||
"""Base SQLAlchemy Model for automatic serialization and
|
||||
deserialization of columns and nested relationships.
|
||||
|
||||
|
@ -54,6 +56,11 @@ class Model(db.Model):
|
|||
kwargs['_force'] = True
|
||||
self._set_columns(**kwargs)
|
||||
|
||||
def filter_by(self, **kwargs):
|
||||
clauses = [key == value
|
||||
for key, value in kwargs.items()]
|
||||
return self.filter(and_(*clauses))
|
||||
|
||||
def _set_columns(self, **kwargs):
|
||||
force = kwargs.get('_force')
|
||||
|
||||
|
@ -283,6 +290,10 @@ class Model(db.Model):
|
|||
db.session.delete(self)
|
||||
db.session.commit()
|
||||
|
||||
@classmethod
|
||||
def query(cls):
|
||||
return db.session.query(cls)
|
||||
|
||||
@classmethod
|
||||
def get_by_id(cls, id_):
|
||||
return cls.query.filter_by(id=id_).first()
|
||||
return cls.query().filter_by(id=id_).first()
|
Loading…
Add table
Add a link
Reference in a new issue