For objects with a PK, it should display instead the value of PK, like
this:
{{{
Out[4]: <Transaction: pk=7>
}}}
If the model has no PK, display like this:
{{{
Out[4]: <Transaction: no pk>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27953>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Victor Porton):
I know that this can be done manually with every model, but I want to do
it automatically for **all** models, to reduce code repeating and do it
always.
--
Ticket URL: <https://code.djangoproject.com/ticket/27953#comment:1>
Comment (by Aymeric Augustin):
Yes, I agree it would be nice to do *something* about this problem.
In your example, `<Transaction: Transaction object>` is the `repr` of the
object, and the `Transaction object` part is the `str` of the object.
Demo:
{{{
>>> class Foo(models.Model):
... class Meta:
... app_label = 'admin'
...
>>> Foo()
<Foo: Foo object>
>>> str(Foo())
'Foo object'
>>> repr(Foo())
'<Foo: Foo object>'
}}}
So we must think about how `__repr__` and `__str__` will behave. Having
`Model.__str__` return `pk=...` regardless of the model type sounds
suboptimal.
--
Ticket URL: <https://code.djangoproject.com/ticket/27953#comment:2>
* type: New feature => Cleanup/optimization
* stage: Unreviewed => Someday/Maybe
Comment:
Here's a past [https://groups.google.com/d/topic/django-
developers/7Jqzwg5nt-c/discussion django-developers discussion] on the
topic. An interested person could revive that thread to try to get a
consensus.
--
Ticket URL: <https://code.djangoproject.com/ticket/27953#comment:3>
* status: new => assigned
* owner: nobody => kapil garg
* stage: Someday/Maybe => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/27953#comment:4>
Comment (by Collin Anderson):
https://github.com/django/django/pull/8336
(1) is my first choice, pk=1 is my second choice. I'd be fine with either.
--
Ticket URL: <https://code.djangoproject.com/ticket/27953#comment:5>
Comment (by Tim Graham <timograham@…>):
In [changeset:"7c9cb1ed37354c184fe0438ecae923d5be711c86" 7c9cb1ed]:
{{{
#!CommitTicketReference repository=""
revision="7c9cb1ed37354c184fe0438ecae923d5be711c86"
Refs #27953 -- Removed hardcoded uses of Model.__str__() in tests.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27953#comment:6>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"1a49b8947033fd667310b1b996330a8e119fcbf9" 1a49b89]:
{{{
#!CommitTicketReference repository=""
revision="1a49b8947033fd667310b1b996330a8e119fcbf9"
Fixed #27953 -- Added instance's pk to Model.__str__().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27953#comment:7>