Opened 15 years ago

Closed 14 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: no

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 14 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by Luc Saffre, 15 years ago

Summary: Error message when __unicode__() returns NoneError message when Model.__unicode__() returns None

comment:2 by Russell Keith-Magee, 15 years ago

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

comment:3 by Matthias Kestenholz, 14 years ago

Needs tests: unset

by Matthias Kestenholz, 14 years ago

Attachment: 12483.diff added

comment:4 by Matthias Kestenholz, 14 years ago

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

Severity: Normal
Type: New feature

comment:6 by Jannis Leidel, 14 years ago

Easy pickings: unset
Triage Stage: AcceptedReady for checkin

comment:7 by Alex Gaynor, 14 years ago

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

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