Why is my sqlalchemy model not callable? - python

I'm following the official sqlalchemy tutorial on getting started, yet when I try to instantiate the User class below, I'm told by the interpreter that it's not callable. I have no idea why. Anyone have any idea?
__init__.py
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
user.py
from sqlalchemy import Column, Integer, String
from __init__ import Base
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
user_api.py
from flask import request
from flask.views import MethodView
from src.main.model.user import User
class UserAPI(MethodView):
def post(self):
usr = User(**request.form)
return usr
__main__.py
import flask
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from src.main.endpoints.user_api import UserAPI
from src.main.model import Base
def database_stuff():
engine = create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker(bind=engine)
Base.metadata.create_all(engine)
if __name__ == "__main__":
database_stuff()
app = flask.Flask(__name__)
user_view = UserAPI.as_view("users")
app.add_url_rule("/users/", defaults={"user_id": None}, view_func=user_view, methods=["GET"])
app.add_url_rule("/users/", view_func=user_view, methods=["POST"])
app.add_url_rule("/users/<int:user_id>", view_func=user_view, methods=["GET"])
app.run()
When attempting to do a post request to the Flask server, this is what I'm getting in the logs:
Traceback (most recent call last):
File "D:\Documents\python\flaskplay\flaskplay\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "D:\Documents\python\flaskplay\flaskplay\lib\site-packages\flask\app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "D:\Documents\python\flaskplay\flaskplay\lib\site-packages\flask\app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "D:\Documents\python\flaskplay\flaskplay\lib\site-packages\flask\app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "D:\Documents\python\flaskplay\flaskplay\lib\site-packages\werkzeug\wrappers.py", line 885, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "D:\Documents\python\flaskplay\flaskplay\lib\site-packages\werkzeug\wrappers.py", line 57, in _run_wsgi_app
return _run_wsgi_app(*args)
File "D:\Documents\python\flaskplay\flaskplay\lib\site-packages\werkzeug\test.py", line 884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'User' object is not callable
127.0.0.1 - - [05/Jun/2017 20:44:32] "POST /users/ HTTP/1.1" 500 -

It actually has nothing to do with sqlalchemy. The object is indeed callable and instantiable. The error actually comes with attempting to return the object as a response. You can't just return any old object from a Flask endpoint. If I convert the object dict to json, then the error is no longer applicable.

Related

not able to create database SQLAlchemy with sqllight

It is the first time when I am using SQLAlchemy with sqllight.
My code looks like this:
#we are going to create the flask app
app = Flask(__name__)
global db
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqllite:///databse.db'
app.config['SECRET_KEY'] = 'ca8b6694c31960d1f7ee2d1ac73c669f'
db = SQLAlchemy(app)
app = create_app()
The next step is to go the terminal and type python. The python interpreter will come and I typed "from main import db", since my file is called main I have to imported from the main.
But this is the actual error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/alex/portal/main.py", line 15, in <module>
db = SQLAlchemy(app)
File "/home/alex/portal/venv/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 219, in __init__
self.init_app(app)
File "/home/alex/portal/venv/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 326, in init_app
engines[key] = self._make_engine(key, options, app)
File "/home/alex/portal/venv/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 614, in _make_engine
return sa.engine_from_config(options, prefix="")
File "/home/alex/portal/venv/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 817, in engine_from_config
return create_engine(url, **options)
File "<string>", line 2, in create_engine
File "/home/alex/portal/venv/lib/python3.10/site-packages/sqlalchemy/util/deprecations.py", line 277, in warned
return fn(*args, **kwargs) # type: ignore[no-any-return]
File "/home/alex/portal/venv/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 556, in create_engine
entrypoint = u._get_entrypoint()
File "/home/alex/portal/venv/lib/python3.10/site-packages/sqlalchemy/engine/url.py", line 754, in _get_entrypoint
cls = registry.load(name)
File "/home/alex/portal/venv/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 365, in load
raise exc.NoSuchModuleError(
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:sqllite
If you have any suggestions please let me know.
Plus I am trying to put this code in the "_ init _.py" from the script in order to have a cleaner code.
You have mis-spelled sqlite as sqllite:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///databse.db'
But even if you fix that you're going to have additional problems, because you're redefining app at the end of your code. Here's a working example, which is a simplified version of this example from the documentation.
from flask import Flask, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///databse.db"
app.config["SECRET_KEY"] = "ca8b6694c31960d1f7ee2d1ac73c669f"
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String)
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
with app.app_context():
db.create_all()
#app.route("/users", methods=["GET"])
def user_list():
res = db.session.execute(db.select(User).order_by(User.username)).scalars()
users = res.fetchall()
return [user.as_dict() for user in users]
#app.route("/users", methods=["POST"])
def user_create():
user = User(**request.get_json())
db.session.add(user)
db.session.commit()
return user.as_dict()
#app.route("/user/<int:id>")
def user_detail(id):
user = db.get_or_404(User, id)
return user.as_dict()
You can create a user entry:
$ curl http://localhost:5000/users -H content-type:application/json \
-d '{"username": "alice", "email": "alice#example.com"}'
{
"email": "alice#example.com",
"id": 1,
"username": "alice"
}
Get a list of users:
$ curl http://localhost:5000/users
[
{
"email": "alice#example.com",
"id": 1,
"username": "alice"
}
]
Get details on a single user:
$ curl http://localhost:5000/user/1
{
"email": "alice#example.com",
"id": 1,
"username": "alice"
}
Etc.

Flask_SQLAlchemy create_all() doesn't work

I started learning how to use SQLAlchemy for my code but for some reason when I ran the code it raised this exception:
Traceback (most recent call last):
File "C:/Users/smadar.KLAG/PycharmProjects/keylogger/site/practice.py", line 118, in <module>
db.create_all()
File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
return connector.get_engine()
File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
options = self.get_options(sa_url, echo)
File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
self._sa.apply_driver_hacks(self._app, sa_url, options)
File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 914, in apply_driver_hacks
sa_url.database = os.path.join(app.root_path, sa_url.database)
AttributeError: can't set attribute
This is the code:
from flask import Flask, redirect, url_for, render_template, request, session, flash
from time import sleep
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.sqlite3'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
class Users(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
email = db.Column(db.String(100))
password = db.Column(db.String(100))
if __name__ == '__main__':
db.create_all()
app.run(debug=True)
I tried running the code of a friend of mine that has SQLAlchemy (the code does work for him) incase it was a coding issue but I have reached the same error with his code as well.
Is there something I am doing incorrectly?
I just ran into this too. Looks like SQLAlchemy just released version 1.4, which breaks flask_sqlalchemy.
I was able to resolve the issue on my system by installing the previous (1.3) version instead.
EDIT: the latest version of SQLAlchemy works now (version 1.4 as of writing this)
installing SQLAlchemy 1.3.23 worked for me.
pip install SQLAlchemy==1.3.23

Flask SQL no such table

I've tried this before and it worked, now I'm doing it again but it doesn't. I use a virtual env.
The db file is in the same directory as the init and models. I've also executed the db.create_all() in the python shell after importing db. The error occurs when I execute Students.query.all(). I'm really confused now. I compared my codes with my existing projects and it is the same. I don't understand why it doesn't work.
Traceback (most recent call last):
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\engine\default.py", line 581, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: students
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3211, in all
return list(self)
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3367, in __iter__
return self._execute_and_instances(context)
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3392, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\engine\base.py", line 982, in execute
return meth(self, multiparams, params)
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1101, in _execute_clauseelement
distilled_params,
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1250, in _execute_context
e, statement, parameters, cursor, context
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "C:\Users\Martin\Desktop\regtry\venv\lib\site-packages\sqlalchemy\engine\default.py", line 581, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: students
[SQL: SELECT students.id AS students_id, students.last_name AS students_last_name, students.college AS students_college, students.email AS students_email
FROM students]
(Background on this error at: http://sqlalche.me/e/e3q8)
_ _ init _ _.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
main = Flask(__name__)
main.config['SECRET_KEY'] = '3bce79429ad6b79670e4e800fb4a57b9'
main.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(main)
from reg import routes
models.py:
from reg import db
class Students(db.Model):
id = db.Column(db.Integer, primary_key=True)
last_name = db.Column(db.String(60), nullable=False)
college = db.Column(db.String(60), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f"Student('{self.last_name}','{self.college}','{self.email}')"
It probably has to do with where you are calling db.create_all(). You have to call it after you have defined all of your models. Here is a working example in one module:
import flask
from flask_sqlalchemy import SQLAlchemy
app = flask.Flask(__name__)
app.config['SECRET_KEY'] = '3bce79429ad6b79670e4e800fb4a57b9'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
class Students(db.Model):
id = db.Column(db.Integer, primary_key=True)
last_name = db.Column(db.String(60), nullable=False)
college = db.Column(db.String(60), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f"Student('{self.last_name}','{self.college}','{self.email}')"
db.create_all()
#app.route("/students", methods=["GET"])
def all_students():
students = Students.query.all()
resp = []
for student in students:
resp.append(dict(
id=student.id,
last_name=student.last_name,
college=student.college,
email=student.email
))
return flask.jsonify(resp), 200
#app.route("/students/<_id>", methods=["GET"])
def student_by_id(_id):
student = Students.query.get(_id)
if student:
resp = dict(
id=student.id,
last_name=student.last_name,
college=student.college,
email=student.email
)
return flask.jsonify(resp), 200
else:
return "", 404
#app.route("/students", methods=["POST"])
def add_student():
req = flask.request.get_json()
student = Students(**req)
db.session.add(student)
db.session.commit()
resp = flask.make_response()
resp.headers["Location"] = flask.url_for("student_by_id", _id=student.id)
return resp, 201
In your example I did not see a call to db.create_all() but I suspect that it is in the wrong spot. Using your example, I believe it will work if you move the call from where you have it to the end of your __init__.py:
__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
main = Flask(__name__)
main.config['SECRET_KEY'] = '3bce79429ad6b79670e4e800fb4a57b9'
main.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(main)
from reg import routes
db.create_all()
In the shell, import the database and the models first before executing db.create_all(). Apparently, error occurs when create you create database without importing the models.
My mistake was:
>>from APPFOLDER import db
>>db.create_all()
>>from APPFOLDER.models import MODELNAME
>>MODELNAME.query.all()
***ERROR MSG***
What you should do:
>>from APPFOLDER import db
>>from APPFOLDER.models import MODELNAME
>>db.create_all()
>>MODELNAME.query.all()
[]

Flask-SQLAlchemy unittests failing InvalidRequestError: Table '{table_name}' is already defined for this MetaData instance

I'm building an API service with Flask, SQLAlchemy and more recently integrating the Flask-SQLAlchemy extension. While I can run the app standalone and make API calls successfully, I am hitting a problem attempting to run unittests. I believe the issue is with importing the db.Model types more than once.
The exception is this:
Error
Traceback (most recent call last):
File "/Users/james/.pyenv/versions/2.7.10/lib/python2.7/unittest/case.py", line 322, in run
self.setUp()
File "/Users/james/Documents/workspace/trustmile-backend/trustmile/tests/test_users.py", line 28, in setUp
from trustmile.app.users.model import User, ConsumerUser, CourierUser, AuthSession, Location, UserAddress, db
File "/Users/james/Documents/workspace/trustmile-backend/trustmile/app/users/model.py", line 23, in <module>
class User(db.Model, UniqueMixin, TableColumnsBase, References):
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 536, in __init__
DeclarativeMeta.__init__(self, name, bases, d)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 55, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
_MapperConfig.setup_mapping(cls, classname, dict_)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
cfg_cls(cls_, classname, dict_)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 131, in __init__
self._setup_table()
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 394, in _setup_table
**table_kw)
File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 398, in __new__
"existing Table object." % key)
InvalidRequestError: Table 'tmuser' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
My init.py in /app/ (the top level) is like this:
__author__ = 'james'
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import config
from app.messaging import EmailHandler
app = Flask(__name__, static_folder='api/static', static_url_path='/static')
app.config.from_object(config)
db = SQLAlchemy(app)
EmailHandler.setup(config.EMAIL_API_KEY_DEV)
from app.api.consumer_v1 import bp as blueprint
app.register_blueprint(blueprint, url_prefix = '/consumer/v1')
app.test_request_context()
Running that with:
from app import app
app.run(debug=True, host='0.0.0.0', port=5001)
Works great.
The beginning of my test_users.py looks like this:
__author__ = 'james'
from trustmile.app.exc import InvalidEmailException
from trustmile.app.exc import InsecurePasswordException
from nose.tools import assert_true, raises
from trustmile.app.users.model import User, ConsumerUser, CourierUser, AuthSession, Location, UserAddress, db
from . import TransactionalTest
email_address = 'james#cloudadvantage.com.au'
test_password = 'mypassword'
class UserTest(TransactionalTest):
#classmethod
def setUpClass(cls):
super(UserTest, cls).setUpClass()
def setUp(self):
super(UserTest, self).setUp()
def test_create_user(self):
user = User()
db.session.add(user)
It must be fairly common way to do unit testing and yes I'm sharing the SQLAlchemy object between modules (which I'm sure is bad practice). I'm running it with nosetests and the error occurs as the code is initialised. Sorry for the lengthy question, hopefully someone can help me!
__table_args__ = {'extend_existing': True}
right below __tablename__

AttributeError: type object 'User' has no attribute 'query'

I have a large database built and populated with sqlalchemy (the original - not flask-SQLAlachemy). I would like to add flask-security to the site, and I realize it relies on flask-SQLAlachemy (instead of the original), but it is my understanding (just started trying to learn more about it) that flask-SQLAlchemy can work with the original. Entering users into the database seems to work. When I navigate to the restricted page, it prompts me to login (as it should). When entering the correct email address and password, I get the error AttributeError: type object 'User' has no attribute 'query', and since the traceback of the error doesn't reference any of my code, I'm not sure how to resolve it.
Thinking this may have been a sqlalchemy to flask-SQLAlchemy conversion issue, I feel like I have tried everything short of creating a new database. I haven't found anything in the flask-security or flask-SQLAlchemy documentation to help resolve this issue. Anyone have an idea on how to resolve this?
Here is my code:
testing.py
from flask import Flask, render_template
from sqlalchemy import create_engine, asc, and_, or_, func, desc
from sqlalchemy.orm import sessionmaker
import database_setup
from database_setup import Base, User, Role
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin, login_required
application.config['SECRET_KEY'] = 'super-secret'
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(database_setup, User, Role)
security = Security(application, user_datastore)
engine = create_engine('sqlite:///landtohuntalpha.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
#application.route('/user/add/', methods = ['GET','POST'])
def addUser():
if request.method == 'POST':
submission = User(
email = request.form['email'],
password = request.form['password'],
)
session.add(submission)
session.commit()
return redirect(url_for('completedUser'))
else:
return render_template('addUser.html')
#application.route('/restricted_page/')
#login_required
def restrictedPage():
return render_template('restricted_page.html')
database_setup.py
import os
import sys
from sqlalchemy import Column, ForeignKey, Integer, String, DateTime, Boolean, Float, Date, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
from flask_security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin, login_required
Base = declarative_base()
roles_users = Table('roles_users', Base.metadata, Column('user_id', Integer, ForeignKey('user.id')), Column('role_id', Integer, ForeignKey('role.id')))
class Role(Base, RoleMixin):
__tablename__ = 'role'
id = Column(Integer, primary_key=True)
name = Column(String(80), unique=True)
description = Column(String(255))
class User(Base, UserMixin):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
email = Column(String(250), nullable=False)
password = Column(String(30))
last_login_at = Column(DateTime)
active = Column(Boolean)
confirmed_at = Column(DateTime)
current_login_at = Column(DateTime)
last_login_ip = Column(String(45))
current_login_ip = Column(String(45))
login_count = Column(Integer)
Traceback Error
File "C:\Python34\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python34\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python34\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python34\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python34\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python34\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Python34\lib\site-packages\flask_security\decorators.py", line 205, in wrapper
return f(*args, **kwargs)
File "C:\Python34\lib\site-packages\flask_security\views.py", line 75, in login
if form.validate_on_submit():
File "C:\Python34\lib\site-packages\flask_wtf\form.py", line 166, in validate_on_submit
return self.is_submitted() and self.validate()
File "C:\Python34\lib\site-packages\flask_security\forms.py", line 230, in validate
self.user = _datastore.get_user(self.email.data)
File "C:\Python34\lib\site-packages\flask_security\datastore.py", line 191, in get_user
rv = self.user_model.query.filter(query).first()
AttributeError: type object 'User' has no attribute 'query'
your User class is inheriting from UserMixin (from flask-login) which doesn't have a query attribute.
can't fully explain the differences between our projects but here is my basic setup:
init.py:
from flask import Flask, render_template, session
from flask.ext.sqlalchemy import SQLAlchemy
import flask.ext.login as FL
# define the main app object
app = Flask(__name__)
app.config.from_object('config')
app.secret_key = 'super secret string'
login_manager = FL.LoginManager()
login_manager.init_app(app)
login_manager.login_view = "/"
# create the database object
db = SQLAlchemy(app)
then in models.py (make sure you import db) make User class inherit from db.Model:
from flasker import db
import datetime
from flask.ext.login import UserMixin
class User(db.Model):
id = db.Column(db.Integer,primary_key=True)
email = db.Column(db.Unicode(128))
name = db.Column(db.Unicode(128))
password = db.Column(db.Unicode(1024))
authenticated = db.Column(db.Boolean, default=False)
posts = db.relationship('Post')
#-----login requirements-----
def is_active(self):
#all users are active
return True
def get_id(self):
# returns the user e-mail. not sure who calls this
return self.email
def is_authenticated(self):
return self.authenticated
def is_anonymous(self):
# False as we do not support annonymity
return False
#constructor
def __init__(self, name=None, email=None, password=None):
self.name = name
self.email = email
self.password = password
self.authenticated = True
Add
Base.query = db_session.query_property()
under
Base = declarative_base()
in
database_setup.py
Ref: https://pythonhosted.org/Flask-Security/quickstart.html#id2
You can try to modify the way you create db Models like this:
class User(db.Model):
# parms
Generally create Models in this way will be able to let User use query method.
Hope it helps.

Categories

Resources