Opened 7 years ago

Closed 5 years ago

#12483 closed New feature (wontfix)

Error message when Model.__unicode__() returns None

Reported by: Luc Saffre 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 Matthias Kestenholz 6 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 7 years ago by Luc Saffre

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Summary: Error message when __unicode__() returns NoneError message when Model.__unicode__() returns None

comment:2 Changed 7 years ago by Russell Keith-Magee

Has patch: set
Needs tests: set
Triage Stage: UnreviewedAccepted

comment:3 Changed 6 years ago by Matthias Kestenholz

Needs tests: unset

Changed 6 years ago by Matthias Kestenholz

Attachment: 12483.diff added

comment:4 Changed 6 years ago by Matthias Kestenholz

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 6 years ago by Matt McClanahan

Severity: Normal
Type: New feature

comment:6 Changed 5 years ago by Jannis Leidel

Easy pickings: unset
Triage Stage: AcceptedReady for checkin

comment:7 Changed 5 years ago by Alex Gaynor

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

Resolution: wontfix
Status: newclosed
Triage Stage: Ready for checkinDesign decision needed

Closing as Alex requested.

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