Opened 6 years ago

Closed 4 years ago

#12483 closed New feature (wontfix)

Error message when Model.__unicode__() returns None

Reported by: lsaffre Owned by: nobody
Component: Core (Other) Version: 1.1
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX:

Description

I had already several times the situation that the unicode method of one of my models returned None in some case.
This leads to a "TypeError: coercing to Unicode: need string or buffer, NoneType found" traceback.
That's okay, but sometimes it is not easy to guess *which* model is wrong.
So I took django/db/models/base.py and added the lines marked '+' below:

    def __repr__(self):
        try:
            u = unicode(self)
        except (UnicodeEncodeError, UnicodeDecodeError):
            u = '[Bad Unicode data]'
    +   except TypeError,e:
    +       raise TypeError("%s: %s" % (self.__class__, e))
        return smart_str(u'<%s: %s>' % (self.__class__.__name__, u))

Attachments (1)

12483.diff (2.2 KB) - added by mk 4 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 6 years ago by lsaffre

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Error message when __unicode__() returns None to Error message when Model.__unicode__() returns None

comment:2 Changed 5 years ago by russellm

  • Has patch set
  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 4 years ago by mk

  • Needs tests unset

Changed 4 years ago by mk

comment:4 Changed 4 years ago by mk

New patch with tests and everything.

I'm not completely sure whether raising a new exception on TypeError is worth it though -- maybe we lose precious trackback information...

comment:5 Changed 4 years ago by mattmcc

  • Severity set to Normal
  • Type set to New feature

comment:6 Changed 4 years ago by jezdez

  • Easy pickings unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:7 Changed 4 years ago by Alex

I'm -1 on this, if you dislike the error message I highly recommended taking this up with upstream CPython (or your VM of choice I suppose), as written this will catch the issue in only some contexts (notably those that try to turn the model into a str() rather than unicode() directly).

comment:8 Changed 4 years ago by jezdez

  • Resolution set to wontfix
  • Status changed from new to closed
  • Triage Stage changed from Ready for checkin to Design decision needed

Closing as Alex requested.

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