1

I have 2 models:

class Restaurant(models.Model):
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)

    def __str__(self):              # __unicode__ on Python 2
        return "%s the restaurant" % self.place.name

class Waiter(models.Model):
    restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

    def __str__(self):              # __unicode__ on Python 2
        return "%s the waiter at %s" % (self.name, self.restaurant)

if I want to get all waiters and restaurants with a single query using join I write:

w = Waiter.objects.select_related().all()

or

w = Waiter.objects.select_related().get(pk=1)

But how could get a restaurant with all waiters? I tried

r = Restaurant.objects.select_related().get(pk=1) 

but it didn't work.

I do not want to do this with 2 db interogations like:

r = Restaurant.objects.get(pk=1)
w = r.waiter_set.all()

It is possible to get all informations with a single query?

Thank you

0 Answers0