TireSwingSoftware / openassign-server

OpenAssign server intended for use by a separate client via RPC
BSD 3-Clause "New" or "Revised" License
6 stars 2 forks source link

Smarter select_related() use #151

Open mhrivnak opened 12 years ago

mhrivnak commented 12 years ago

In Getter's __init__ method, there is some logic to determine which fields should be added to a select_related() call. It looks at the requested fields, looks at the corresponding getter type defined on the manager, and depending on the type it finds, may add that field name to the foreign_keys list.

This isn't helpful for derived properties like AchievementAwardManager's "achievement_name". It would be nice to have a dictionary on the AchievementAwardManager where we have keys of field names exposed through the API and values of the corresponding model field that should be added to the select_related call.

To illustrate the problem:

g = Getter('', facade.managers.AchievementAwardManager, facade.models.AchievementAward.objects.all(), ['date', 'achievement_name'], False)
g = Getter('', facade.managers.AchievementAwardManager, facade.models.AchievementAward.objects.all().select_related('achievement'), ['date', 'achievement_name'], False)

The first line results in n+1 SQL queries where n is the number of achievement awards. The second line results in just 1 SQL query.

I propose something like this:

class AchievementAwardManager(ObjectManager):
    ADDITIONAL_SELECT_RELATED_FIELDS = {'achievement_name' : 'achievement'}
mhrivnak commented 12 years ago

This will also be useful for Credential.credential_type_name