0

Below is my models.py

from . import db

class Product(db.Model):
    __tablename__='Products'
    id=db.Column(db.Integer,primary_key=True)
    brandname=db.Column(db.String(64),unique=True)
    description=db.Column(db.String(500),nullable=False)
    price=db.Column(db.Float,nullable=False)
    image=db.Column(db.String(64),nullable=False,default='defaultproduct.jpg')

    def __repr__(self):
        str = "Id: {}, brandname: {}, description: {}, price: {}, image: {} \n"
        str = str.format(self.id,self.brandname,self.description,self.price,self.image)
        return str 

below is my views.py

from flask import Blueprint, render_template, url_for , request , session
from .models import Product

bp = Blueprint('main',__name__)

@bp.route('/')
def index():
    products= Product.query.order_by(Product.brandname).all()
    return render_template('Home.html',products=products)

when I try to run the project I get error like "AttributeError: 'function' object has no attribute 'query'"

Can anyone helpme to resolve this issue ?

1 Answer 1

1

You are not using Python class structures properly. Your Product class has no attribute query, meaning that "query" is a meaningless statement in the class Product. If you want to be able to use query in Product you must first define it in the Product class, for example:

class Product(db.Model):
    __tablename__='Products'
    id=db.Column(db.Integer,primary_key=True)
    brandname=db.Column(db.String(64),unique=True)
    description=db.Column(db.String(500),nullable=False)
    price=db.Column(db.Float,nullable=False)
    query = some_value
    image=db.Column(db.String(64),nullable=False,default='defaultproduct.jpg')

Looking at your code, it looks like you're trying use Python as a DBMS, which it is not. You do not need to "query" anything in a class, you simply reference it by the following logic: Class.attribute.

Please read the Python documentation on classes: https://docs.python.org/3/tutorial/classes.html

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.