flask-sqlalchemy:AttributeError:type object没有属性'query',在ipython中工作

时间:2015-09-12 03:31:15

标签: python flask flask-sqlalchemy flask-restful

我正在创建一个新的烧瓶应用程序,使用flask-sqlalchemy和flask-restful with Python 3.4。我已经定义了我的用户模型:

from mytvpy import db
from sqlalchemy.ext.declarative import declared_attr


class BaseModel(db.Model):
    __abstract__ = True

    id = db.Column(db.Integer, primary_key=True)
    created = db.Column(db.TIMESTAMP, server_default=db.func.now())
    last_updated = db.Column(db.TIMESTAMP, server_default=db.func.now(), onupdate=db.func.current_timestamp())

    @declared_attr
    def __tablename__(cls):
        return cls.__name__


class User(BaseModel):

    username = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    email = db.Column(db.String(255), unique=True)

    def __init__(self, username, password, email):
        super(User, self).__init__()
        self.username = username
        self.password = password
        self.email = email

如果我尝试在ipython中查询,它可以工作:

In [15]: from mytvpy.models.base import User

In [16]: User.query.all()
Out[16]: [<mytvpy.models.base.User at 0x7fac65b1c6a0>]

但如果我尝试从端点点击它:

class User(Resource):

    def get(self, user):
        return User.query.filter(User.username==user).scalar()

它疯了:

Traceback (most recent call last):
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
    return original_handler(e)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/_compat.py", line 32, in reraise
    raise value.with_traceback(tb)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
    return original_handler(e)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/_compat.py", line 32, in reraise
    raise value.with_traceback(tb)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask_restful/__init__.py", line 471, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/rich/anaconda/envs/mytv/lib/python3.5/site-packages/flask_restful/__init__.py", line 581, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/rich/prj/mytv/mytvpy/blueprints/base.py", line 12, in get
    return User.query.filter(User.username==user).scalar()
AttributeError: type object 'User' has no attribute 'query'

1 个答案:

答案 0 :(得分:7)

您有两个名为Use r的类,一个扩展BaseModel,另一个扩展Resource。后者正在影响前者。

更改导入/引用模型的方式,您的代码将起作用。

from mytvpy.models import base

base.User.query.all()