Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#1055 closed defect (fixed)

Models should have better default __str__() and __repr__() methods

Reported by: GomoX <gomo@…> Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: gomo@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Right now, the default __repr__() method as defined in the Model class (in django/core/meta/ looks like:

def __repr__(self):
    return '<%s object>' % self.__class__.__name__

I think we should have at least a __str__() definition and a better __repr__(), which includes at least the object id, so that when using the interactive interpreter, you can tell an object from another. Something like:

def __str__(self):
    return '%s %s' % (self.__class__.__name__,
def __repr__(self):
    return '<%s object ID=%s>' % (self.__class__.__name__,

Change History (3)

comment:1 Changed 13 years ago by anonymous


comment:2 Changed 13 years ago by Antti Kaihola

The annoying thing about <> -enclosed Python-style repr strings is that when inserting them in templates for debugging you must remember to escape them to actually see them on the page.

comment:3 Changed 13 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

This was fixed a while back.

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