Sunday, December 13, 2020

Re: How to access using foreing key

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)


On Oct 30, 2020, at 9:52 AM, Ashutosh Mishra <ashutoshmishra333@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+unsubscribe@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.

No comments:

Post a Comment