Opened 13 years ago

Last modified 12 years ago

#16173 closed Cleanup/optimization

manager.get on foreign key fields (related fields) — at Version 1

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

Description (last modified by Aymeric Augustin)

__get__ on ForeignKey field uses manager to query the values.

The code is as given below:

            # If the related manager indicates that it should be used for
            # related fields, respect that.
            rel_mgr = self.field.rel.to._default_manager
            db = router.db_for_read(self.field.rel.to, instance=instance)            
            if getattr(rel_mgr, 'use_for_related_fields', False):
                rel_obj = rel_mgr.using(db).get(**params)


Check the last line rel_obj = rel_mgr.using(db).get(**params) for using rel_mgr with multiple databases. rel_mgr.using(db) returns a queryset. If developer has written a custom manager class for which get is overriden, then custom managers get function will never get called. This results in surprises to the developer. I think this line should changed to:

    rel_obj = rel_mgr.db_manager(db).get(**params)

Change History (1)

comment:1 by Aymeric Augustin, 13 years ago

Description: modified (diff)
UI/UX: unset

Fixed wiki formatting.

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