Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #7378 -- Use the "to_field" where appropriate on reverse relati…

…ons.

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


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7785 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c17e3265852722d572614b96b8bda1105ee8352e 1 parent 4931639
Malcolm Tredinnick malcolmt authored
24 django/db/models/fields/related.py
View
@@ -185,11 +185,11 @@ def __get__(self, instance, instance_type=None):
def __set__(self, instance, value):
if instance is None:
raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
-
- # The similarity of the code below to the code in
+
+ # The similarity of the code below to the code in
# ReverseSingleRelatedObjectDescriptor is annoying, but there's a bunch
# of small differences that would make a common base class convoluted.
-
+
# If null=True, we can assign null here, but otherwise the value needs
# to be an instance of the related class.
if value is None and self.related.field.null == False:
@@ -197,14 +197,14 @@ def __set__(self, instance, value):
(instance._meta.object_name, self.related.get_accessor_name()))
elif value is not None and not isinstance(value, self.related.model):
raise ValueError('Cannot assign "%r": "%s.%s" must be a "%s" instance.' %
- (value, instance._meta.object_name,
+ (value, instance._meta.object_name,
self.related.get_accessor_name(), self.related.opts.object_name))
-
+
# Set the value of the related field
setattr(value, self.related.field.rel.get_related_field().attname, instance)
# Since we already know what the related object is, seed the related
- # object caches now, too. This avoids another db hit if you get the
+ # object caches now, too. This avoids another db hit if you get the
# object you just set.
setattr(instance, self.cache_name, value)
setattr(value, self.related.field.get_cache_name(), instance)
@@ -243,7 +243,7 @@ def __get__(self, instance, instance_type=None):
def __set__(self, instance, value):
if instance is None:
raise AttributeError, "%s must be accessed via instance" % self._field.name
-
+
# If null=True, we can assign null here, but otherwise the value needs
# to be an instance of the related class.
if value is None and self.field.null == False:
@@ -251,9 +251,9 @@ def __set__(self, instance, value):
(instance._meta.object_name, self.field.name))
elif value is not None and not isinstance(value, self.field.rel.to):
raise ValueError('Cannot assign "%r": "%s.%s" must be a "%s" instance.' %
- (value, instance._meta.object_name,
+ (value, instance._meta.object_name,
self.field.name, self.field.rel.to._meta.object_name))
-
+
# Set the value of the related field
try:
val = getattr(value, self.field.rel.get_related_field().attname)
@@ -262,7 +262,7 @@ def __set__(self, instance, value):
setattr(instance, self.field.attname, val)
# Since we already know what the related object is, seed the related
- # object cache now, too. This avoids another db hit if you get the
+ # object cache now, too. This avoids another db hit if you get the
# object you just set.
setattr(instance, self.field.get_cache_name(), value)
@@ -322,7 +322,9 @@ def clear(self):
clear.alters_data = True
manager = RelatedManager()
- manager.core_filters = {'%s__pk' % rel_field.name: getattr(instance, rel_field.rel.get_related_field().attname)}
+ attname = rel_field.rel.get_related_field().name
+ manager.core_filters = {'%s__%s' % (rel_field.name, attname):
+ getattr(instance, attname)}
manager.model = self.related.model
return manager
8 tests/regressiontests/queries/models.py
View
@@ -368,6 +368,10 @@ class Child(models.Model):
>>> Author.objects.filter(report__name='r1')
[<Author: a1>]
+Bug #7378
+>>> a1.report_set.all()
+[<Report: r1>]
+
Bug #5324, #6704
>>> Item.objects.filter(tags__name='t4')
[<Item: four>]
@@ -777,8 +781,8 @@ class Child(models.Model):
>>> out = pickle.dumps(Item.objects.all())
Bug #7277
->>> a1 = Annotation.objects.create(name='a1', tag=t1)
->>> a1.notes.add(n1)
+>>> ann1 = Annotation.objects.create(name='a1', tag=t1)
+>>> ann1.notes.add(n1)
>>> n1.annotation_set.filter(Q(tag=t5) | Q(tag__children=t5) | Q(tag__children__children=t5))
[<Annotation: a1>]
Please sign in to comment.
Something went wrong with that request. Please try again.