#1055 closed defect (fixed)
Models should have better default __str__() and __repr__() methods
Reported by: | 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 , 19 years ago
comment:2 by , 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.
Note:
See TracTickets
for help on using tickets.
П