#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 )
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 , 7 years ago
Cc: | added |
---|---|
Description: | modified (diff) |
Summary: | Model.__repr__ should include the pk → Model.__repr__() should include the pk |
Type: | New feature → Cleanup/optimization |
comment:2 by , 7 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
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 , 7 years ago
Resolution: | duplicate |
---|---|
Status: | closed → new |
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 , 7 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
The place to revisit the decision would be on the django-developers thread. Comments in this ticket tracker don't reach a wide audience.
comment:5 by , 7 years ago
For completion sake: https://groups.google.com/d/msg/django-developers/7Jqzwg5nt-c/DqyXOBy3BQAJ
This is effectively fixed (unless
Model.__str__()
is overridden) in Django 2.0 by #27953 which includes the pk in the defaultModel.__str__()
. I'm not sure if further changes should be made.There's some related discussion in #20448.