为了账号安全,请及时绑定邮箱和手机立即绑定

flask高级教程报错询问

/ 猿问

flask高级教程报错询问

JERRY 2019-05-23 20:04:31
from datetime import datetime
from contextlib import contextmanager
from sqlalchemy import Column, Integer, SmallInteger
from flask import current_app
from flask_sqlalchemy import SQLAlchemy as _SQLAlchemy, BaseQuery

__all__ = ['db', 'Base']


class SQLAlchemy(_SQLAlchemy):
    @contextmanager
    def auto_commit(self, throw=True):
        try:
            yield
            self.session.commit()
        except Exception as e:
            self.session.rollback()
            current_app.logger.exception('%r' % e)
            if throw:
                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 BaseMixin(object):
#     def __getitem__(self, key):
#         return getattr(self, key)


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())

    @property
    def create_datetime(self):
        if self.create_time:
            return datetime.fromtimestamp(self.create_time)
        else:
            return None

    def delete(self):
        self.status = 0

    def set_attrs(self, attrs):
        for key, value in attrs.items():
            if hasattr(self, key) and key != 'id':
                setattr(self, key, value)


class BaseNoCreateTime(db.Model):
    __abstract__ = True
    status = Column(SmallInteger, default=1)

Traceback (most recent call last):

  File "/Users/hank/Downloads/ResultDashboard/manager.py", line 5, in <module>

    from app import app

  File "/Users/hank/Downloads/ResultDashboard/app/__init__.py", line 9, in <module>

    from app.models import db

  File "/Users/hank/Downloads/ResultDashboard/app/models/__init__.py", line 5, in <module>

    from .base import *

  File "/Users/hank/Downloads/ResultDashboard/app/models/base.py", line 48, in <module>

    db = SQLAlchemy(query_class=Query)

TypeError: __init__() got an unexpected keyword argument 'query_class'


查看完整描述

1 回答

?
JERRY

为什么会报这个错 有没有大佬帮我看下啊

查看完整回答
反对 回复 2019-05-23

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信