from django.db.models import Count from .models import Tag, Product, ProductOnBill, Receipt, Shop, ShopTag def get_general_stats(user, date_range=None): out = { "receipts": user.receipts.all().count(), "products": user.products.all().count(), "shops": user.shops.all().count(), "purchases": user.products_on_bills.all().count(), "product_tags": user.tags.all().count(), "shop_tags": user.shop_tags.all().count(), } return out def get_shops_stats(user): def shop_cout(name): return user.receipts.all().filter(shop__name=name).count() out = [{"name": s.name, "count": shop_cout(s.name)} for s in user.shops.all()] return out def get_products_stats(user): # def shop_cout(name): # return user..all().filter(pro__name=name).count() out = [ {"name": "product", "count": "times bought"} ] return out