title: Flask-Security AttributeError: 'NoneType' object has no attribute 'filter_by'
body: <p>I'm working on a computer grid dashboard and ran into the following error a few times:</p>
  
  <pre class="lang-sh prettyprint-override"><code>AttributeError: 'NoneType' object has no attribute 'filter_by'
  </code></pre>
  
  <p>This happens after I change my model a little bit.
  The original model is:</p>
  
  <pre class="lang-py prettyprint-override"><code>class Worker (UserMixin, Base):
  
    __tablename__ = 'worker'
  
    ...
  </code></pre>
  
  <p>and when it's exactly like this I get the following:</p>
  
  <pre class="lang-sh prettyprint-override"><code>[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 &lt;lambda&gt;
      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'
  </code></pre>
  
  <p>Looking for this error on the Internet, I get the answer that it should inherit also from <code>db.Model</code>, being <code>db = SQLAlchemy(app)</code>. But then I'd create an "import loop", once I'd need to import <code>Worker</code> to app from models, and <code>db</code> to models from app. This obviously crashes.</p>
  
  <p>A workaround I thought would work was to import the Model object like</p>
  
  <pre class="lang-py prettyprint-override"><code>from flask_sqlalchemy import Model
  </code></pre>
  
  <p>and make my model inherit from it</p>
  
  <pre class="lang-py prettyprint-override"><code>class Worker (Model, UserMixin, Base):
    ...
  </code></pre>
  
  <p>but finally I get the following:</p>
  
  <pre class="lang-sh prettyprint-override"><code>[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 &lt;lambda&gt;
      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'
  </code></pre>
  
  <p>How can I make this right? I'm just furious about it and can't focus anymore</p>