Error message when Model.__unicode__() returns None
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))
Change History
(9)
Summary: |
Error message when __unicode__() returns None → Error message when Model.__unicode__() returns None
|
Has patch: |
set
|
Needs tests: |
set
|
Triage Stage: |
Unreviewed → Accepted
|
Severity: |
→ Normal
|
Type: |
→ New feature
|
Easy pickings: |
unset
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ wontfix
|
Status: |
new → closed
|
Triage Stage: |
Ready for checkin → Design decision needed
|
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...