#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)
Change History (12)
by , 14 years ago
Attachment: | admin-relatedobject.patch added |
---|
comment:1 by , 14 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 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 , 14 years ago
Attachment: | admin_related_obj_test.diff added |
---|
Tests only that show the error before the patch
comment:3 by , 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 , 14 years ago
Attachment: | admin_related_obj_combined.diff added |
---|
Added a combined patch with the fix and tests.
comment:4 by , 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 , 14 years ago
Thanks d0ugal. We only saw this problem in 1.2.1, after upgrading from 1.1.2.
comment:6 by , 14 years ago
milestone: | → 1.3 |
---|
comment:7 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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).