Code

Ticket #7904: use_for_related_fields.diff

File use_for_related_fields.diff, 934 bytes (added by jbronn, 6 years ago)

Patch that looks for use_for_related_fields attribute on managers.

Line 
1Index: django/db/models/fields/related.py
2===================================================================
3--- django/db/models/fields/related.py  (revision 8154)
4+++ django/db/models/fields/related.py  (working copy)
5@@ -239,7 +239,14 @@
6                 params = {'%s__pk' % self.field.rel.field_name: val}
7             else:
8                 params = {'%s__exact' % self.field.rel.field_name: val}
9-            rel_obj = QuerySet(self.field.rel.to).get(**params)
10+
11+            # If the related manager indicates that it should be used for
12+            # related fields, respect that.
13+            rel_mgr = self.field.rel.to._default_manager
14+            if getattr(rel_mgr, 'use_for_related_fields', False):
15+                rel_obj = rel_mgr.get(**params)
16+            else:
17+                rel_obj = QuerySet(self.field.rel.to).get(**params)
18             setattr(instance, cache_name, rel_obj)
19             return rel_obj
20