Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#1055 closed defect (fixed)

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

Reported by: GomoX <gomo@…> Owned by: adrian
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: UI/UX:

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)

Attachments (0)

Change History (3)

comment:1 Changed 8 years ago by anonymous

&#1055

comment:2 Changed 8 years ago by akaihola

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 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

This was fixed a while back.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.