Changeset 7785
- Timestamp:
- 06/29/08 05:35:35 (2 months ago)
- Files:
-
- django/trunk/django/db/models/fields/related.py (modified) (6 diffs)
- django/trunk/tests/regressiontests/queries/models.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/fields/related.py
r7778 r7785 186 186 if instance is None: 187 187 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 190 190 # ReverseSingleRelatedObjectDescriptor is annoying, but there's a bunch 191 191 # of small differences that would make a common base class convoluted. 192 192 193 193 # If null=True, we can assign null here, but otherwise the value needs 194 194 # to be an instance of the related class. … … 198 198 elif value is not None and not isinstance(value, self.related.model): 199 199 raise ValueError('Cannot assign "%r": "%s.%s" must be a "%s" instance.' % 200 (value, instance._meta.object_name, 200 (value, instance._meta.object_name, 201 201 self.related.get_accessor_name(), self.related.opts.object_name)) 202 202 203 203 # Set the value of the related field 204 204 setattr(value, self.related.field.rel.get_related_field().attname, instance) 205 205 206 206 # 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 208 208 # object you just set. 209 209 setattr(instance, self.cache_name, value) … … 244 244 if instance is None: 245 245 raise AttributeError, "%s must be accessed via instance" % self._field.name 246 246 247 247 # If null=True, we can assign null here, but otherwise the value needs 248 248 # to be an instance of the related class. … … 252 252 elif value is not None and not isinstance(value, self.field.rel.to): 253 253 raise ValueError('Cannot assign "%r": "%s.%s" must be a "%s" instance.' % 254 (value, instance._meta.object_name, 254 (value, instance._meta.object_name, 255 255 self.field.name, self.field.rel.to._meta.object_name)) 256 256 257 257 # Set the value of the related field 258 258 try: … … 263 263 264 264 # 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 266 266 # object you just set. 267 267 setattr(instance, self.field.get_cache_name(), value) … … 323 323 324 324 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)} 326 328 manager.model = self.related.model 327 329 django/trunk/tests/regressiontests/queries/models.py
r7780 r7785 368 368 >>> Author.objects.filter(report__name='r1') 369 369 [<Author: a1>] 370 371 Bug #7378 372 >>> a1.report_set.all() 373 [<Report: r1>] 370 374 371 375 Bug #5324, #6704 … … 778 782 779 783 Bug #7277 780 >>> a 1 = Annotation.objects.create(name='a1', tag=t1)781 >>> a 1.notes.add(n1)784 >>> ann1 = Annotation.objects.create(name='a1', tag=t1) 785 >>> ann1.notes.add(n1) 782 786 >>> n1.annotation_set.filter(Q(tag=t5) | Q(tag__children=t5) | Q(tag__children__children=t5)) 783 787 [<Annotation: a1>]
