Let's say I have two models that look like this:
class ProductType (models.Model):
product_type = models.CharField (max_length = 100)
class Product (models.Model):
name = models.CharField (max_length = 200)
slug = models.SlugField ()
product_type = models.ForeignKey (ProductType)
score = models.PositiveIntegerField (default = 0)
Now I want to get two of the best products (two with the highest score) from each ProductType. Therefore, if I have Phones, Computers, TVs , like ProductTypes, I want the two best phones, computers, TVs.
Since I don’t even know how to do this in MySQL, I tried to find a MySQL solution, but the ones I find are extremely complicated, and it is not very difficult to do.
I tend to create my own model for the best products and have a cronjob to fix this, but I would like to see if there is an easier solution for this.
rinti source
share