Opened 16 years ago

Last modified 8 years ago

#9394 closed

Querying a many-to-many intermediate model from a manager on a multi-table inherited model produces extraneous queries — at Initial Version

Reported by: Erin Kelly Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

With the following models:

class Place(models.Model):
    name = models.CharField(max_length=50)

class Business(Place):
    owner = models.CharField(max_length=50)

class Restaurant(Business):
    rating = models.IntegerField()

class Chef(models.Model):
    name = models.CharField(max_length=50)
    restaurants = models.ManyToManyField(ChineseRestaurant, through='Employee')

class Employee(models.Model):
    restaurant = models.ForeignKey(ChineseRestaurant)
    chef = models.ForeignKey(Chef)
    years_of_service = models.IntegerField()

we can do some_restaurant.employee_set.all(), which results in three queries. The first two queries are just retrieving the attributes of the inherited Business and Place models, which is unnecessary since all that information already exists on the some_restaurant object. Only one query should be needed.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top