Ticket #13793: one_to_one_use_default_manager_if_asked.diff

File one_to_one_use_default_manager_if_asked.diff, 1.0 KB (added by Shaun Cutts, 14 years ago)

patch for SingleRelatedObjectDescriptor

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 5830a79..b7c9223 100644
    a b class SingleRelatedObjectDescriptor(object):  
    223223        except AttributeError:
    224224            params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
    225225            db = router.db_for_read(self.related.model, instance=instance)
    226             rel_obj = self.related.model._base_manager.using(db).get(**params)
     226           
     227            # use default manager if it wants to be used for related fields, or
     228            # base manager otherwise
     229            rel_model = self.related.model
     230            rel_mgr = rel_model._default_manager
     231            if not getattr(rel_mgr, 'use_for_related_fields', False):
     232                rel_mgr = rel_model._base_manager
     233            rel_obj = rel_mgr.using(db).get(**params)
    227234            setattr(instance, self.cache_name, rel_obj)
    228235            return rel_obj
    229236
Back to Top