Skip to content

Commit

Permalink
Fixed #13963 -- Use the correct verbose name of a reverse relation fi…
Browse files Browse the repository at this point in the history
…eld in the admin. Thanks, sfllaw and d0ugal.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14244 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jezdez committed Oct 17, 2010
1 parent 400125d commit 214dc97
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
7 changes: 6 additions & 1 deletion django/contrib/admin/util.py
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models from django.db import models
from django.db.models.related import RelatedObject
from django.forms.forms import pretty_name from django.forms.forms import pretty_name
from django.utils import formats from django.utils import formats
from django.utils.html import escape from django.utils.html import escape
Expand Down Expand Up @@ -278,7 +279,11 @@ def lookup_field(name, obj, model_admin=None):
def label_for_field(name, model, model_admin=None, return_attr=False): def label_for_field(name, model, model_admin=None, return_attr=False):
attr = None attr = None
try: try:
label = model._meta.get_field_by_name(name)[0].verbose_name field = model._meta.get_field_by_name(name)[0]
if isinstance(field, RelatedObject):
label = field.opts.verbose_name
else:
label = field.verbose_name
except models.FieldDoesNotExist: except models.FieldDoesNotExist:
if name == "__unicode__": if name == "__unicode__":
label = force_unicode(model._meta.verbose_name) label = force_unicode(model._meta.verbose_name)
Expand Down
15 changes: 13 additions & 2 deletions tests/regressiontests/admin_util/models.py
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,5 @@
from django.db import models from django.db import models




class Article(models.Model): class Article(models.Model):
""" """
A simple Article model for testing A simple Article model for testing
Expand All @@ -20,3 +18,16 @@ def test_from_model_with_override(self):


class Count(models.Model): class Count(models.Model):
num = models.PositiveSmallIntegerField() num = models.PositiveSmallIntegerField()

class Event(models.Model):
date = models.DateTimeField(auto_now_add=True)

class Location(models.Model):
event = models.OneToOneField(Event, verbose_name='awesome event')

class Guest(models.Model):
event = models.OneToOneField(Event)
name = models.CharField(max_length=255)

class Meta:
verbose_name = "awesome guest"
19 changes: 18 additions & 1 deletion tests/regressiontests/admin_util/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.utils import unittest from django.utils import unittest
from django.utils.formats import localize from django.utils.formats import localize


from models import Article, Count from models import Article, Count, Event, Location




class NestedObjectsTests(TestCase): class NestedObjectsTests(TestCase):
Expand Down Expand Up @@ -219,3 +219,20 @@ def test_from_model(self, obj):
), ),
("not Really the Model", MockModelAdmin.test_from_model) ("not Really the Model", MockModelAdmin.test_from_model)
) )

def test_related_name(self):
"""
Regression test for #13963
"""
self.assertEquals(
label_for_field('location', Event, return_attr=True),
('location', None),
)
self.assertEquals(
label_for_field('event', Location, return_attr=True),
('awesome event', None),
)
self.assertEquals(
label_for_field('guest', Event, return_attr=True),
('awesome guest', None),
)

0 comments on commit 214dc97

Please sign in to comment.