web-dev-qa-db-ja.com

フラスコ-sqlalchemy:AttributeError:タイプオブジェクトに属性 'query'がなく、ipythonで動作します

新しいflaskアプリを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'
11
postelrich

Userという名前の2つのクラスがあります。1つはBaseModelを拡張し、もう1つはResourceを拡張します。後者は前者を隠しています。

モデルのインポート/参照方法を変更すると、コードが機能するはずです。

from mytvpy.models import base

base.User.query.all()
10
dirn