Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #13963 -- Use the correct verbose name of a reverse rel…

…ation field in the admin. Thanks, sfllaw and d0ugal.

Backport from trunk (r14244).

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14245 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0e4737ec240f144dc072d17e99b12143d872e577 1 parent 2a33115
Jannis Leidel authored October 17, 2010
7  django/contrib/admin/util.py
... ...
@@ -1,5 +1,6 @@
1 1
 from django.core.exceptions import ObjectDoesNotExist
2 2
 from django.db import models
  3
+from django.db.models.related import RelatedObject
3 4
 from django.forms.forms import pretty_name
4 5
 from django.utils import formats
5 6
 from django.utils.html import escape
@@ -279,7 +280,11 @@ def lookup_field(name, obj, model_admin=None):
279 280
 def label_for_field(name, model, model_admin=None, return_attr=False):
280 281
     attr = None
281 282
     try:
282  
-        label = model._meta.get_field_by_name(name)[0].verbose_name
  283
+        field = model._meta.get_field_by_name(name)[0]
  284
+        if isinstance(field, RelatedObject):
  285
+            label = field.opts.verbose_name
  286
+        else:
  287
+            label = field.verbose_name
283 288
     except models.FieldDoesNotExist:
284 289
         if name == "__unicode__":
285 290
             label = force_unicode(model._meta.verbose_name)
15  tests/regressiontests/admin_util/models.py
... ...
@@ -1,7 +1,5 @@
1 1
 from django.db import models
2 2
 
3  
-
4  
-
5 3
 class Article(models.Model):
6 4
     """
7 5
     A simple Article model for testing
@@ -20,3 +18,16 @@ def test_from_model_with_override(self):
20 18
 
21 19
 class Count(models.Model):
22 20
     num = models.PositiveSmallIntegerField()
  21
+
  22
+class Event(models.Model):
  23
+    date = models.DateTimeField(auto_now_add=True)
  24
+
  25
+class Location(models.Model):
  26
+    event = models.OneToOneField(Event, verbose_name='awesome event')
  27
+
  28
+class Guest(models.Model):
  29
+    event = models.OneToOneField(Event)
  30
+    name = models.CharField(max_length=255)
  31
+
  32
+    class Meta:
  33
+        verbose_name = "awesome guest"
19  tests/regressiontests/admin_util/tests.py
@@ -12,7 +12,7 @@
12 12
 from django.contrib.sites.models import Site
13 13
 from django.contrib.admin.util import NestedObjects
14 14
 
15  
-from models import Article, Count
  15
+from models import Article, Count, Event, Location
16 16
 
17 17
 
18 18
 class NestedObjectsTests(TestCase):
@@ -220,3 +220,20 @@ def test_from_model(self, obj):
220 220
             ),
221 221
             ("not Really the Model", MockModelAdmin.test_from_model)
222 222
         )
  223
+
  224
+    def test_related_name(self):
  225
+        """
  226
+        Regression test for #13963
  227
+        """
  228
+        self.assertEquals(
  229
+            label_for_field('location', Event, return_attr=True),
  230
+            ('location', None),
  231
+        )
  232
+        self.assertEquals(
  233
+            label_for_field('event', Location, return_attr=True),
  234
+            ('awesome event', None),
  235
+        )
  236
+        self.assertEquals(
  237
+            label_for_field('guest', Event, return_attr=True),
  238
+            ('awesome guest', None),
  239
+        )

0 notes on commit 0e4737e

Please sign in to comment.
Something went wrong with that request. Please try again.