1👍
✅
Product
and ProductRating
are not related through a field/key, so you can’t order Product
objects based on ProductRating
.
You can modify the Product
model to include a ForeignKey to ProductsRatings
like the following:
class Products(models.Model):
name = models.CharField(max_length=300)
category = models.CharField(max_length=300)
cost = models.IntegerField()
rating = models.ForeignKey(ProductsRating, on_delete=models.SET_NULL)
class ProductsRating(models.Model):
is_it_good = models.IntegerField(validators=[MaxValueValidator(0), MinValueValidator(5)])
whose_rated = models.ForeignKey(CusUser, on_delete=models.CASCADE)
Then, your queryset would look like this:
qs = Products.objects.annotate(
biggest_rating = Max('rating__is_it_good')
).order_by('biggest_rating')
Source:stackexchange.com