Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28839 closed Cleanup/optimization (duplicate)

Model.__repr__() should include the pk

Reported by: Álex Córcoles Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Collin Anderson Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

This was proposed on https://code.djangoproject.com/ticket/1055 , but it was fixed without including the PK. It was also suggested in https://code.djangoproject.com/ticket/20448 , but ignored.

I think the PK is very valuable as __repr__, as it allows you to retrieve the object unambiguously; while str really can't have a good default implementation.

Change History (5)

comment:1 by Tim Graham, 7 years ago

Cc: Collin Anderson added
Description: modified (diff)
Summary: Model.__repr__ should include the pkModel.__repr__() should include the pk
Type: New featureCleanup/optimization

This is effectively fixed (unless Model.__str__() is overridden) in Django 2.0 by #27953 which includes the pk in the default Model.__str__(). I'm not sure if further changes should be made.

There's some related discussion in #20448.

comment:2 by Álex Córcoles, 7 years ago

Resolution: duplicate
Status: newclosed

Doh, sorry, I did not find #27953. Just tested it and it works perfectly, I'm closing this as a duplicate myself.

comment:3 by Álex Córcoles, 7 years ago

Resolution: duplicate
Status: closednew

Sorry again, after taking a look at the implementation, I disagree slightly; the PK should be included in repr, not str. It's nice to have a default repr implementation with the PK for use in the shell et al., but str is used for stuff such as choosing the displayed str on a dropdown- where I think a PK is out of place...

(You might close this again if you guys disagree- I'm OK with that, but I wanted to make my full case).

comment:4 by Tim Graham, 7 years ago

Resolution: duplicate
Status: newclosed

The place to revisit the decision would be on the django-developers thread. Comments in this ticket tracker don't reach a wide audience.

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