If you are looking for all of the Restaurants with a 5.0 rating, it looks like

Restaurant.objects.filter(rating_restaurant__rating = 5.0)

If you are looking for Restaurants with an average rating of 5.0, that’s a bit 
trickier.

from django.db.models import Avg
Restaurant.objects.annotate(avg_rating = 
Avg('rating_restaurant__rating')).filter(avg_rating__eq = 5.0)

You can find some surprisingly relevant examples at 
https://docs.djangoproject.com/en/2.2/topics/db/aggregation/#order-of-annotate-and-filter-clauses
 
<https://docs.djangoproject.com/en/2.2/topics/db/aggregation/#order-of-annotate-and-filter-clauses>.

> On Oct 30, 2020, at 9:52 AM, Ashutosh Mishra <ashutoshmishra...@gmail.com> 
> wrote:
> 
> class User(models.Model):
> name=models.CharField(max_length=50,blank=True,null=True)
> latitude = models.CharField('Latitude', max_length=30, blank=True, null=True)
> longitude = models.CharField('Longitude', max_length=30, blank=True, 
> null=True)
> location = models.PointField(blank=True, null=True)
> 
> def __str__(self):
> return str(self.name)
> 
> class Restaurant(models.Model):
> restaurant_name=models.CharField(max_length=50,blank=True,null=True)
> latitude = models.CharField('Latitude', max_length=30, blank=True, null=True)
> longitude = models.CharField('Longitude', max_length=30, blank=True, 
> null=True)
> location = models.PointField(blank=True, null=True)
> 
> def __str__(self):
> return str(self.restaurant_name)
> 
> 
> class RestaurantRating(models.Model):
> user = 
> models.ForeignKey(User,on_delete=models.CASCADE,null=True,related_name="customer_rating")
> restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE, 
> null=True, related_name="rating_restaurant")
> rating = models.FloatField()
> 
> def __str__(self):
> return str(self.user)
> 
> How can i filter the restaurants whose rating is 5,Restaurant.objects.filter()
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com 
> <mailto:django-users+unsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/cdaa9adf-5b4e-4de7-9c9d-f9e875e17acbn%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/cdaa9adf-5b4e-4de7-9c9d-f9e875e17acbn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/A7CC40B1-C3AB-4744-85F7-4CD7CB6454C7%40gmail.com.

Reply via email to