Opened 19 years ago

Closed 19 years ago

Last modified 18 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

Description

Right now, the default __repr__() method as defined in the Model class (in django/core/meta/__init__.py) 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__, self.id)
def __repr__(self):
    return '<%s object ID=%s>' % (self.__class__.__name__, self.id)

Change History (3)

comment:1 by anonymous, 19 years ago

&#1055

comment:2 by Antti Kaihola, 19 years ago

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 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

This was fixed a while back.

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