Flask-Security AttributeError:“ NoneType”对象没有属性“ filter_by”

时间:2020-03-13 18:18:32

标签: python flask flask-sqlalchemy flask-login flask-security

我正在计算机网格仪表板上工作,几次遇到以下错误:

AttributeError: 'NoneType' object has no attribute 'filter_by'

这是在我稍微改变一下模型之后发生的。 原始模型是:

class Worker (UserMixin, Base):

  __tablename__ = 'worker'

  ...

当它完全像这样时,我得到以下信息:

[2020-03-13 15:10:11,389] ERROR in app: Exception on /admin/ [GET]
Traceback (most recent call last):
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib64/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib64/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 1947, in full_dispatch_request
    rv = self.preprocess_request()
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 2241, in preprocess_request
    rv = func()
  File "/home/rancher/.local/lib/python3.6/site-packages/flask_principal.py", line 477, in _on_before_request
    identity = loader()
  File "/home/rancher/.local/lib/python3.6/site-packages/flask_security/core.py", line 245, in _identity_loader
    if not isinstance(current_user._get_current_object(), AnonymousUserMixin):
  File "/home/rancher/.local/lib/python3.6/site-packages/werkzeug/local.py", line 307, in _get_current_object
    return self.__local()
  File "/usr/local/lib64/python3.6/site-packages/flask_login/utils.py", line 26, in <lambda>
    current_user = LocalProxy(lambda: _get_user())
  File "/usr/local/lib64/python3.6/site-packages/flask_login/utils.py", line 335, in _get_user
    current_app.login_manager._load_user()
  File "/usr/local/lib64/python3.6/site-packages/flask_login/login_manager.py", line 359, in _load_user
    return self.reload_user()
  File "/usr/local/lib64/python3.6/site-packages/flask_login/login_manager.py", line 321, in reload_user
    user = self.user_callback(user_id)
  File "/home/rancher/.local/lib/python3.6/site-packages/flask_security/core.py", line 221, in _user_loader
    return _security.datastore.find_user(id=user_id)
  File "/home/rancher/.local/lib/python3.6/site-packages/flask_security/datastore.py", line 254, in find_user
    return self.user_model.query.filter_by(**kwargs).first()
AttributeError: type object 'Worker' has no attribute 'query'

在Internet上寻找此错误,我得到的答案是它也应从db.Model(即db = SQLAlchemy(app))继承。但是,一旦我需要将Worker从模型导入到应用程序,并将db从应用程序导入到模型,便会创建一个“导入循环”。这显然崩溃了。

我认为可行的解决方法是导入Model对象,例如

from flask_sqlalchemy import Model

并使我的模型从中继承

class Worker (Model, UserMixin, Base):
  ...

但最终我得到以下信息:

[2020-03-13 15:06:45,878] ERROR in app: Exception on /admin/ [GET]
Traceback (most recent call last):
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib64/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib64/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 1947, in full_dispatch_request
    rv = self.preprocess_request()
  File "/usr/local/lib64/python3.6/site-packages/flask/app.py", line 2241, in preprocess_request
    rv = func()
  File "/home/rancher/.local/lib/python3.6/site-packages/flask_principal.py", line 477, in _on_before_request
    identity = loader()
  File "/home/rancher/.local/lib/python3.6/site-packages/flask_security/core.py", line 245, in _identity_loader
    if not isinstance(current_user._get_current_object(), AnonymousUserMixin):
  File "/home/rancher/.local/lib/python3.6/site-packages/werkzeug/local.py", line 307, in _get_current_object
    return self.__local()
  File "/usr/local/lib64/python3.6/site-packages/flask_login/utils.py", line 26, in <lambda>
    current_user = LocalProxy(lambda: _get_user())
  File "/usr/local/lib64/python3.6/site-packages/flask_login/utils.py", line 335, in _get_user
    current_app.login_manager._load_user()
  File "/usr/local/lib64/python3.6/site-packages/flask_login/login_manager.py", line 359, in _load_user
    return self.reload_user()
  File "/usr/local/lib64/python3.6/site-packages/flask_login/login_manager.py", line 321, in reload_user
    user = self.user_callback(user_id)
  File "/home/rancher/.local/lib/python3.6/site-packages/flask_security/core.py", line 221, in _user_loader
    return _security.datastore.find_user(id=user_id)
  File "/home/rancher/.local/lib/python3.6/site-packages/flask_security/datastore.py", line 254, in find_user
    return self.user_model.query.filter_by(**kwargs).first()
AttributeError: 'NoneType' object has no attribute 'filter_by'

我该怎么做?我对此很生气,无法集中注意力

2 个答案:

答案 0 :(得分:2)

这有时也使我发疯。

在模型文件中: db = SQLAlchemy()

class Worker(db.Model,UserMixin)(不确定什么是Base)

然后在您的应用文件中:

从工作人员导入数据库

db.init_app(app)

答案 1 :(得分:0)

尝试使用

//complexity n^2
def checkRotation(string1: String, string2: String): Boolean = {
  for (i <- 0 until string1.length) yield {
    for (j <- 0 until string2.length) yield {
      if (string1.charAt(i) == string2.charAt(j)) {
        string1 == string2.substring(j) + "" + string2.substring(0, j)
      } else {
        false
      }
    }
  }
}.flatten.contains(true)