Skip to content

Smarter select_related() use #151

@mhrivnak

Description

@mhrivnak

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'}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions