Bootstrap FreeKB - Flask - Resolve "AttributeError: 'function' object has no attribute 'query'"
Flask - Resolve "AttributeError: 'function' object has no attribute 'query'"

Updated:   |  Flask articles

Let's say the following is being returned.

AttributeError: 'function' object has no attribute 'query'

 

I once had this issue when I had a function with the same name as my model class. For example, my models class was named users.

class users(db.Model, UserMixin):
    id              = db.Column(db.Integer,     nullable=False, unique=True, primary_key=True)
    date_created    = db.Column(db.DateTime(timezone=True), default=func.now())
    date_updated    = db.Column(db.DateTime(timezone=True), onupdate=func.now()) 
    email           = db.Column(db.String(100), nullable=False, unique=True)
    password        = db.Column(db.String(200), nullable=False, unique=False)

 

And this error started up after I mistakenly created a function also named users. This fix was simply, I just had to give my function some other name.

@auth.route('/Users')
def users():
    data = users.query.filter_by(email='john.doe@example.com')
    print("user = " + str(data.email))
    return

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 5fd46f in the box below so that we can be sure you are a human.