Opened 14 years ago

Closed 13 years ago

Last modified 12 years ago

#13963 closed (fixed)

Admin: 'RelatedObject' object has no attribute 'verbose_name'

Reported by: Simon Law Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Keywords: OneToOneField RelatedObject list_display
Cc: simon@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Create some models that look like this:

from django.contrib import admin
from django.db import models


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


class EventAdmin(admin.ModelAdmin):
    list_display = ('date', 'location')  # Reverse OneToOneField lookup
admin.site.register(Event, EventAdmin)


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

If you go to the admin page for Events, you will get this traceback:

  File "/home/sfllaw/hg/akoha/work/eggs/Django-1.2.1-py2.6.egg/django/contrib/admin/util.py", line 282, in label_for_field
    label = model._meta.get_field_by_name(name)[0].verbose_name
TemplateSyntaxError: Caught AttributeError while rendering: 'RelatedObject' object has no attribute 'verbose_name'

Included is a patch that prevents this. When the admin is trying to list a RelatedObject, it should use the var_name instead, as it is more natural.

Attachments (3)

admin-relatedobject.patch (657 bytes ) - added by Simon Law 14 years ago.
admin_related_obj_test.diff (1.9 KB ) - added by Dougal Matthews 14 years ago.
Tests only that show the error before the patch
admin_related_obj_combined.diff (2.6 KB ) - added by Dougal Matthews 14 years ago.
Added a combined patch with the fix and tests.

Download all attachments as: .zip

Change History (12)

by Simon Law, 14 years ago

Attachment: admin-relatedobject.patch added

comment:1 by Carl Meyer, 14 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

I can confirm the issue (though the Location model definition has to be moved above the EventAdmin definition for the above sample models to validate). Patch fixes the issue and looks reasonable.

Simon, if you'd like to move this forward what's necessary now is an automated test (probably in tests/regression_tests/admin_util) that fails before your patch and passes after it. (Also, of course, ensuring that no existing tests fail with the patch).

comment:2 by Simon Law, 14 years ago

I'm afraid that I don't have time to write up a proper patch right now. If someone can get this done sooner than a few months, please be my guest.

by Dougal Matthews, 14 years ago

Attachment: admin_related_obj_test.diff added

Tests only that show the error before the patch

comment:3 by Dougal Matthews, 14 years ago

Needs tests: unset

The added tests (in admin_related_obj_test.diff) will show the error before the patch and pass after applying the attached patch.

by Dougal Matthews, 14 years ago

Added a combined patch with the fix and tests.

comment:4 by Dougal Matthews, 14 years ago

I was unable to reproduce the problem on version 1.1.2 but was able to on both 1.2.1 and trunk.

comment:5 by Simon Law, 14 years ago

Thanks d0ugal. We only saw this problem in 1.2.1, after upgrading from 1.1.2.

comment:6 by anonymous, 13 years ago

milestone: 1.3

comment:7 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: newclosed

(In [14244]) Fixed #13963 -- Use the correct verbose name of a reverse relation field in the admin. Thanks, sfllaw and d0ugal.

comment:8 by Jannis Leidel, 13 years ago

(In [14245]) [1.2.X] Fixed #13963 -- Use the correct verbose name of a reverse relation field in the admin. Thanks, sfllaw and d0ugal.

Backport from trunk (r14244).

comment:9 by Jacob, 12 years ago

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top