[Django] #27953: Models should display IDs

8 views
Skip to first unread message

Django

unread,
Mar 17, 2017, 11:37:05 AM3/17/17
to django-...@googlegroups.com
#27953: Models should display IDs
-------------------------------------+-------------------------------------
Reporter: Victor | Owner: nobody
Porton |
Type: New | Status: new
feature |
Component: Database | Version: 1.10
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
In [4]: t
Out[4]: <Transaction: Transaction object>
}}}

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.

Django

unread,
Mar 17, 2017, 11:38:02 AM3/17/17
to django-...@googlegroups.com
#27953: Models should display IDs
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Mar 17, 2017, 11:41:12 AM3/17/17
to django-...@googlegroups.com
#27953: Models should display IDs
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Mar 17, 2017, 1:17:06 PM3/17/17
to django-...@googlegroups.com
#27953: Make default Model.__str__() more useful (such as by displaying the primary
key)

-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* 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>

Django

unread,
Apr 3, 2017, 4:08:55 PM4/3/17
to django-...@googlegroups.com
#27953: Make default Model.__str__() more useful (such as by displaying the primary
key)
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: kapil
Type: | garg
Cleanup/optimization | Status: assigned

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by kapil garg):

* status: new => assigned
* owner: nobody => kapil garg
* stage: Someday/Maybe => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/27953#comment:4>

Django

unread,
Apr 8, 2017, 2:40:42 PM4/8/17
to django-...@googlegroups.com
#27953: Make default Model.__str__() more useful (such as by displaying the primary
key)
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: kapil
Type: | garg
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 9, 2017, 1:29:17 PM6/9/17
to django-...@googlegroups.com
#27953: Make default Model.__str__() more useful (such as by displaying the primary
key)
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: kapil
Type: | garg
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 9, 2017, 2:41:30 PM6/9/17
to django-...@googlegroups.com
#27953: Make default Model.__str__() more useful (such as by displaying the primary
key)
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: kapil
Type: | garg
Cleanup/optimization | Status: closed

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages