Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#13963 closed (fixed)

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

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

Download all attachments as: .zip

Change History (12)

Changed 5 years ago by sfllaw

comment:1 Changed 5 years ago by carljm

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 5 years ago by sfllaw

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 5 years ago by d0ugal

Tests only that show the error before the patch

comment:3 Changed 5 years ago by d0ugal

  • 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 5 years ago by d0ugal

Added a combined patch with the fix and tests.

comment:4 Changed 5 years ago by d0ugal

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 5 years ago by sfllaw

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

comment:6 Changed 5 years ago by anonymous

  • milestone set to 1.3

comment:7 Changed 5 years ago by jezdez

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:8 Changed 5 years ago by jezdez

(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 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

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