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 Version 2

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 (last modified by Ramiro Morales)

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(Restaurant, through='Employee')

class Employee(models.Model):
    restaurant = models.ForeignKey(Restaurant)
    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 (2)

comment:1 by Erin Kelly, 16 years ago

The occurrences of ChineseRestaurant in the models should just be Restaurant. Sorry about that.

comment:2 by Ramiro Morales, 16 years ago

Description: modified (diff)

(edited code example in the description as per Ian's note above, replacing ChineseRestaurant with Restaurant.)

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