Django

Code

Changeset 8212

Show
Ignore:
Timestamp:
08/05/08 11:13:28 (5 months ago)
Author:
jacob
Message:

Fixed #7904: added support for a "use_for_related_fields" property on managers. If True, the manager will be used for related object lookups instead of the "bare" QuerySet? introduced bu [8107]. Patch from Justin Bronn.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/related.py

    r8191 r8212  
    240240            else: 
    241241                params = {'%s__exact' % self.field.rel.field_name: val} 
    242             rel_obj = QuerySet(self.field.rel.to).get(**params) 
     242 
     243            # If the related manager indicates that it should be used for 
     244            # related fields, respect that. 
     245            rel_mgr = self.field.rel.to._default_manager 
     246            if getattr(rel_mgr, 'use_for_related_fields', False): 
     247                rel_obj = rel_mgr.get(**params) 
     248            else: 
     249                rel_obj = QuerySet(self.field.rel.to).get(**params) 
    243250            setattr(instance, cache_name, rel_obj) 
    244251            return rel_obj 
  • django/trunk/docs/model-api.txt

    r8191 r8212  
    15081508an inability to retrieve objects you'd like to work with. 
    15091509 
     1510Using managers for related object access 
     1511~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     1512 
     1513By default, Django uses a "bare" (i.e. default) manager when accessing related 
     1514objects (i.e. ``choice.poll``). If this default isn't appropriate for your default manager, you can force Django to use a custom manager for related object attributes by giving it a ``use_for_related_fields`` property:: 
     1515 
     1516    class MyManager(models.Manager):: 
     1517        use_for_related_fields = True 
     1518         
     1519        ... 
     1520 
    15101521Model methods 
    15111522============= 
  • django/trunk/tests/regressiontests/reverse_single_related/models.py

    r8017 r8212  
    2727>>> private_item = Item.objects.create(source=private_source) 
    2828 
    29 Only one source is available via all() due to the custom default manager. 
     29# Only one source is available via all() due to the custom default manager. 
    3030 
    3131>>> Source.objects.all() 
     
    3535<Source: Source object> 
    3636 
    37 Make sure that an item can still access its related source even if the default 
    38 manager doesn't normally allow it. 
     37# Make sure that an item can still access its related source even if the default 
     38# manager doesn't normally allow it. 
    3939 
    4040>>> private_item.source 
    4141<Source: Source object> 
    4242 
     43# If the manager is marked "use_for_related_fields", it'll get used instead 
     44# of the "bare" queryset. Usually you'd define this as a property on the class, 
     45# but this approximates that in a way that's easier in tests. 
     46 
     47>>> Source.objects.use_for_related_fields = True 
     48>>> private_item = Item.objects.get(pk=private_item.pk) 
     49>>> private_item.source 
     50Traceback (most recent call last): 
     51    ... 
     52DoesNotExist: Source matching query does not exist. 
     53 
    4354"""}