I have two tables named categories and products. How can I get three records from each category on the homepage? Thanks in advance
class Category(models.Model):
name = ...
class Produc(models.Model):
name = ...
category = models.ForeignKey(Category, ...
class Home_Cls(TemplateView):
...
def homepro(self):
cats = Category.objects.all()
for cat in cats :
que= Products.objects.filter(
category_id=cat[..]
).order_by('?')[:3]
return que
I tried a little. :( Waiting for your help.
You can try this way by storing in list and returning that list:
class Home_Cls(TemplateView):
...
def homepro(self):
cats = Category.objects.all()
que_lst = []
for cat in cats :
que_lst.extend(list(Products.objects.filter(category_id=cat[..]).order_by('?')[:3]))
return que_lst