diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 58e8b82a7a..e3677c2f55 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -165,7 +165,7 @@ class SingleRelatedObjectDescriptor(object): # SingleRelatedObjectDescriptor instance. def __init__(self, related): self.related = related - self.cache_name = '_%s_cache' % related.field.name + self.cache_name = '_%s_cache' % related.get_accessor_name() def __get__(self, instance, instance_type=None): if instance is None: diff --git a/tests/regressiontests/one_to_one_regress/models.py b/tests/regressiontests/one_to_one_regress/models.py index c5ffd3fb3c..c68fdfc780 100644 --- a/tests/regressiontests/one_to_one_regress/models.py +++ b/tests/regressiontests/one_to_one_regress/models.py @@ -15,6 +15,13 @@ class Restaurant(models.Model): def __unicode__(self): return u"%s the restaurant" % self.place.name +class Bar(models.Model): + place = models.OneToOneField(Place) + serves_cocktails = models.BooleanField() + + def __unicode__(self): + return u"%s the bar" % self.place.name + class Favorites(models.Model): name = models.CharField(max_length = 50) restaurants = models.ManyToManyField(Restaurant) @@ -34,4 +41,13 @@ __test__ = {'API_TESTS':""" >>> f.restaurants = [r] >>> f.restaurants.all() [] + +# Regression test for #7173: Check that the name of the cache for the +# reverse object is correct. +>>> b = Bar(place=p1, serves_cocktails=False) +>>> b.save() +>>> p1.restaurant + +>>> p1.bar + """}