Opened 6 years ago

Closed 6 years ago

Last modified 5 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: UI/UX:

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 6 years ago.
admin_related_obj_test.diff (1.9 KB) - added by Dougal Matthews 6 years ago.
Tests only that show the error before the patch
admin_related_obj_combined.diff (2.6 KB) - added by Dougal Matthews 6 years ago.
Added a combined patch with the fix and tests.

Download all attachments as: .zip

Change History (12)

Changed 6 years ago by Simon Law

Attachment: admin-relatedobject.patch added

comment:1 Changed 6 years ago by Carl Meyer

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 Changed 6 years ago by Simon Law

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.

Changed 6 years ago by Dougal Matthews

Attachment: admin_related_obj_test.diff added

Tests only that show the error before the patch

comment:3 Changed 6 years ago by Dougal Matthews

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.

Changed 6 years ago by Dougal Matthews

Added a combined patch with the fix and tests.

comment:4 Changed 6 years ago by Dougal Matthews

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 Changed 6 years ago by Simon Law

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

comment:6 Changed 6 years ago by anonymous

milestone: 1.3

comment:7 Changed 6 years ago by Jannis Leidel

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 Changed 6 years ago by Jannis Leidel

(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 Changed 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

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