Python Forum

Full Version: Flask-Sqlalchemy count products in specific category
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi , I want to make a product counter in specific category. I searched but i didn't find. Please help

This is my models.py

class Products(db.Model):
    __searchable__ = ["product_name"]
    id = db.Column(db.Integer, primary_key=True)
    product_name = db.Column(db.String(20))
    product_amount = db.Column(db.Integer)
    product_price = db.Column(db.Float)
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    category = db.relationship('Category', backref="category")

    def __repr__(self):
        return f"User('{self.username}' '{self.email}')"


class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    category_name = db.Column(db.String(80))

    def __repr__(self):
        return f"{self.category_name}"
This is my app.py

def categories():
    categories = Category.query.all()
    if request.method == "POST":
        category_name = request.form["category_name"]
        category = Category(category_name=category_name)
        db.session.add(category)
        db.session.commit()
        return redirect(url_for("categories"))
    return render_template("categories.html", categories=categories)
Also table


{% for category in categories %}
          <tr>
              <th scope="row">{{ category.id }}</th>
              <th scope="row">{{ category.category_name }}</th>
              <th scope="row">Product Counter in Specific Category</th>
              <td><a href="/update_category/{{ category.id }}" class="btn btn-primary  ">Update</a></td>
              <td><a href="/delete_category/{{ category.id }}" class="btn btn-danger">Delete</a></td>
          </tr>
          {% endfor %}  
Here's the code I use (outside of flask, for desktop app)
you can add the appropriate flask code and your table and column names.

from sqlalchemy.orm import sessionmaker
from sqlalchemy import func
from sqlalchemy import distinct
import DbModel


class CountItems:
    def __init__(self):
    self.model = DbModel
    db = self.model.engine
    self.session = sessionmaker(bind=db)
    self.session.configure(bind=db)

    def get_county_count(self):
        session = self.Session()
        session.query(func.count(self.er_table.CountyCd)).first()
        county_count = session.query(func.count(self.er_table.CountyCd)).first()
        session.close()
        print(f"County count: {county_count}")


if __name__ == '__main__':
    ci = CountItems()
    ci.get_county_count()
(Mar-12-2020, 07:43 PM)Larz60+ Wrote: [ -> ]Here's the code I use (outside of flask, for desktop app)
you can add the appropriate flask code and your table and column names.

from sqlalchemy.orm import sessionmaker
from sqlalchemy import func
from sqlalchemy import distinct
import DbModel


class CountItems:
    def __init__(self):
    self.model = DbModel
    db = self.model.engine
    self.session = sessionmaker(bind=db)
    self.session.configure(bind=db)

    def get_county_count(self):
        session = self.Session()
        session.query(func.count(self.er_table.CountyCd)).first()
        county_count = session.query(func.count(self.er_table.CountyCd)).first()
        session.close()
        print(f"County count: {county_count}")


if __name__ == '__main__':
    ci = CountItems()
    ci.get_county_count()


Man thanks a lot but I use flask_sqlalchemy SQLAlchemy. In this case probably i cant use session.query . I try it but it didnt work. What should i do ?