Django

Code

Changeset 7785

Show
Ignore:
Timestamp:
06/29/08 05:35:35 (2 months ago)
Author:
mtredinnick
Message:

Fixed #7378 -- Use the "to_field" where appropriate on reverse relations.

Patch from mturtle@gmail.com. The remaining uses of "%spk" in
fields/related.py all look safe, since they are for many-to-many fields, which
doesn't take "to_field" as a parameter.

Files:

Legend:

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

    r7778 r7785  
    186186        if instance is None: 
    187187            raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name 
    188          
    189         # The similarity of the code below to the code in  
     188 
     189        # The similarity of the code below to the code in 
    190190        # ReverseSingleRelatedObjectDescriptor is annoying, but there's a bunch 
    191191        # of small differences that would make a common base class convoluted. 
    192          
     192 
    193193        # If null=True, we can assign null here, but otherwise the value needs 
    194194        # to be an instance of the related class. 
     
    198198        elif value is not None and not isinstance(value, self.related.model): 
    199199            raise ValueError('Cannot assign "%r": "%s.%s" must be a "%s" instance.' % 
    200                                 (value, instance._meta.object_name,  
     200                                (value, instance._meta.object_name, 
    201201                                 self.related.get_accessor_name(), self.related.opts.object_name)) 
    202          
     202 
    203203        # Set the value of the related field 
    204204        setattr(value, self.related.field.rel.get_related_field().attname, instance) 
    205205 
    206206        # Since we already know what the related object is, seed the related 
    207         # object caches now, too. This avoids another db hit if you get the  
     207        # object caches now, too. This avoids another db hit if you get the 
    208208        # object you just set. 
    209209        setattr(instance, self.cache_name, value) 
     
    244244        if instance is None: 
    245245            raise AttributeError, "%s must be accessed via instance" % self._field.name 
    246          
     246 
    247247        # If null=True, we can assign null here, but otherwise the value needs 
    248248        # to be an instance of the related class. 
     
    252252        elif value is not None and not isinstance(value, self.field.rel.to): 
    253253            raise ValueError('Cannot assign "%r": "%s.%s" must be a "%s" instance.' % 
    254                                 (value, instance._meta.object_name,  
     254                                (value, instance._meta.object_name, 
    255255                                 self.field.name, self.field.rel.to._meta.object_name)) 
    256          
     256 
    257257        # Set the value of the related field 
    258258        try: 
     
    263263 
    264264        # Since we already know what the related object is, seed the related 
    265         # object cache now, too. This avoids another db hit if you get the  
     265        # object cache now, too. This avoids another db hit if you get the 
    266266        # object you just set. 
    267267        setattr(instance, self.field.get_cache_name(), value) 
     
    323323 
    324324        manager = RelatedManager() 
    325         manager.core_filters = {'%s__pk' % rel_field.name: getattr(instance, rel_field.rel.get_related_field().attname)} 
     325        attname = rel_field.rel.get_related_field().name 
     326        manager.core_filters = {'%s__%s' % (rel_field.name, attname): 
     327                getattr(instance, attname)} 
    326328        manager.model = self.related.model 
    327329 
  • django/trunk/tests/regressiontests/queries/models.py

    r7780 r7785  
    368368>>> Author.objects.filter(report__name='r1') 
    369369[<Author: a1>] 
     370 
     371Bug #7378 
     372>>> a1.report_set.all() 
     373[<Report: r1>] 
    370374 
    371375Bug #5324, #6704 
     
    778782 
    779783Bug #7277 
    780 >>> a1 = Annotation.objects.create(name='a1', tag=t1) 
    781 >>> a1.notes.add(n1) 
     784>>> ann1 = Annotation.objects.create(name='a1', tag=t1) 
     785>>> ann1.notes.add(n1) 
    782786>>> n1.annotation_set.filter(Q(tag=t5) | Q(tag__children=t5) | Q(tag__children__children=t5)) 
    783787[<Annotation: a1>]