Home > Software engineering >  Nonetype Object is not callable - while quering on a database [closed]
Nonetype Object is not callable - while quering on a database [closed]

Time:10-05

I am very new to Flask. While performing query on a database, it throws the following error. Can you please help me to understand why the error is issued?

C:\Users\tamim\Documents\Python_projects\user_login_sql\launch.py:7: SyntaxWarning: 'NoneType' object is not callable; perhaps you missed a comma?

if User.query.filter_by(username='John').first() is None(): Traceback (most recent call last):

File "C:\Users\tamim\Documents\Python_projects\user_login_sql\launch.py", line 7, in if User.query.filter_by(username='John').first() is None(): TypeError: 'NoneType' object is not callable

I am trying to use SQLAlchemy with Flask instead of following Flask-SQLAlchemy. The module which is used to launch the app is below:

from app import create_app, db
from app.models import User

if __name__=='__main__':
    app = create_app('development')
    '''add a default user'''
    if User.query.filter_by(username='John').first() is None():
        User.register('John', 'Cat')
    app.run()

this is the code inside the init.py of app

    import os
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_login import LoginManager
#from sqlalchemy.orm import sessionmaker, scoped_session
from .sqlalchemy_bind import Flask_sqlalchemy


bootstrap = Bootstrap()
db = Flask_sqlalchemy()
lm = LoginManager()
lm.login_view = 'main.login'

def create_app(config_name):
    '''Create an application instance'''
    app = Flask(__name__)

    #import configuration
    cfg = os.path.join(os.getcwd(), 'configure', config_name '.py')
    app.config.from_pyfile(cfg)

    #initialize extensions
    from .models import User
    db.init_app(app)

    bootstrap.init_app(app)

    lm.init_app(app)

    #import blueprints
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)


    return app

The code for models is below -

from werkzeug.security import generate_password_hash, check_password_hash
from . import db, lm
from flask_login import UserMixin
from sqlalchemy import Column, Integer, String

class User(UserMixin, db.Base):
    __tablename__ ='users'
    id = Column(Integer, primary_key=True)
    username = Column(String(16), index=True, unique=True)
    password_hash = Column(String)

    def __init__(self, username = None, password = None):
        self.username = username
        self.password = password

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)
    @staticmethod
    def register(username, password):
        user = User(username=username)
        user.set_password(password)
        db.session.add(user)
        db.session.commit()

    def __repr__(self):
        return '<User {0}>'.format(self.username)

@lm.user_loader
def load_user(id):
    return User.query.get(int(id))

The original code to use SQLAlchemy with Flask is taken from https://github.com/klfoulk16/flask-sqlalchemy-bind

CodePudding user response:

None is not a function/method. So please change your login filter condition to somewhat like this:

if User.query.filter_by(username='John').first() is None:
        User.register('John', 'Cat')
  • Related