Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smarter select_related() use #151

Open
mhrivnak opened this issue Apr 3, 2012 · 1 comment
Open

Smarter select_related() use #151

mhrivnak opened this issue Apr 3, 2012 · 1 comment

Comments

@mhrivnak
Copy link
Member

mhrivnak commented Apr 3, 2012

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
Copy link
Member Author

mhrivnak commented Apr 3, 2012

This will also be useful for Credential.credential_type_name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant