TypeError: __init__() got an unexpected keyword argument 'query_class'
来源:10-13 重写filter_by
SuccessorSocialism
2018-04-12
#models/base.py from datetime import datetime from flask_sqlalchemy import SQLAlchemy as _SQLAlchemy ,BaseQuery from sqlalchemy import Column, SmallInteger, Integer from contextlib import contextmanager class SQLAlchemy(_SQLAlchemy): @contextmanager def auto_commit(self): try: yield self.session.commit() except Exception as e: db.session.rollback() raise e class Query(BaseQuery): def filter_by(self, **kwargs): if 'status' not in kwargs.keys(): kwargs['status'] = 1 return super(Query, self).filter_by(**kwargs) db = SQLAlchemy(query_class = Query) class Base(db.Model): __abstract__ = True create_time = Column('create_time',Integer) status = Column(SmallInteger,default=1) def __init__(self): self.create_time = int(datetime.now().timestamp) def set_attrs(self,attrs_dict): for key,value in attrs_dict.items(): if hasattr(self,key) and key != 'id': setattr(self,key,value) def create_datetime(self): if self.create_time: return datetime.fromtimestamp(self.create_time) else: return None
写回答
3回答
-
你这什么版本的flask sqlalcheny?
042018-04-13 -
SuccessorSocialism
提问者
2018-04-13
class Model(object): """Baseclass for custom user models.""" #: the query class used. The :attr:`query` attribute is an instance #: of this class. By default a :class:`BaseQuery` is used. query_class = BaseQuery #: an instance of :attr:`query_class`. Can be used to query the #: database for instances of this model. query = None class SQLAlchemy(object): def __init__(self, app=None, use_native_unicode=True, session_options=None): self.use_native_unicode = use_native_unicode if session_options is None: session_options = {} session_options.setdefault( 'scopefunc', connection_stack.__ident_func__ ) self.session = self.create_scoped_session(session_options) self.Model = self.make_declarative_base() self._engine_lock = Lock() if app is not None: self.app = app self.init_app(app) else: self.app = None _include_sqlalchemy(self) _MapperSignalEvents(self.mapper).register() _SessionSignalEvents().register() self.Query = BaseQuery # 老师我的 query_class没有在SQLAlchemy的构造函数中。。。在它上方的model中
00 -
7七月
2018-04-13
我看了下代码貌似是没有问题的,能不能调试进入到SQLAlchemy的构造函数中看下?
00
相似问题