Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#19421 closed Bug (worksforme)

When RelatedModel.__unicode__ fails, InlineModelAdmin are not displayed

Reported by: benjaoming Owned by: nobody
Component: contrib.admin Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


Example code:

class RelatedModel:
    def __unicode__(self):
        return self.invalid_property

class SomeInline(admin.TabularInline):
    model = RelatedModel

class SomeAdmin(admin.ModelAdmin):
    inlines = [SomeInline]

When RelatedModel.__unicode__ is called, an exception occurs but is silently ignored. The inlines work fine when adding new objects with SomeAdmin, since __unicode__ is not called here. But when editing an object, the inlines simply disappear!

Expected result: An exception should occur.

Similar experiences here:

Change History (5)

comment:1 Changed 3 years ago by benjaoming

  • Component changed from Uncategorized to contrib.admin
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

comment:2 Changed 3 years ago by russellm

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

Can't reproduce. With models of

class Author(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return u'Author called %s' %

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author)

    def __unicode__(self):
        return u'Book called %s' % self.invalid

and admin registration:

class BookInline(admin.TabularInline):
    model = Book

class AuthorAdmin(admin.ModelAdmin):
    inlines = [BookInline], AuthorAdmin)

I get a full page exception, reading

AttributeError at /admin/testapp/author/1/
'Book' object has no attribute 'invalid'

Which is exactly what I'd expect to see.

If you can provide models that cause the error, feel free to reopen.

comment:3 Changed 3 years ago by anonymous

Confirmed that I'm unable to produce this. Will reopen if I get the scenario right again.

comment:4 Changed 3 years ago by lukeplant

I had something like this not long ago, but found it depended on the value of DEBUG. With DEBUG==True, everything worked fine, but with DEBUG==False you got silent failure and the inline simply disappeared.

In my case, it was a UnicodeDecodeError or UnicodeEncodeError in the __unicode__ method. I ran out of time debugging in.

comment:5 Changed 3 years ago by benjaoming

Yesterday, I also ran out of time trying to debug this on an invalid ascii string ("æøå" instead of u"æøå"). It was fed to a proxy object (ugettext_lazy) inside a template tag, and when the rendering happened, it only failed with DEBUG=False.

Something strange happens in the template compiler, in this case using classy-tags and a template tag from django-cms. Somehow, a DjangoUnicodeDecodeError is lost when DEBUG=True, but replacing that exception (in django.utils.encoding) with a different exception class, and it gets raised.

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