Opened 13 years ago
Last modified 13 years ago
#16173 closed Cleanup/optimization
manager.get on foreign key fields (related fields) — at Initial Version
Reported by: | 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
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)