[Django] #22531: Q objects lack a useful repr

2 views
Skip to first unread message

Django

unread,
Apr 28, 2014, 4:14:45 AM4/28/14
to django-...@googlegroups.com
#22531: Q objects lack a useful repr
----------------------------------------------+--------------------
Reporter: Keryn Knight <django@…> | Owner: nobody
Type: New feature | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
{{{
>>> from django.db.models.query_utils import Q
>>> a = Q()
>>> b = Q(a=1, b=2)
>>> c = b & Q(c=3)
>>> d = c | Q(d=4)
>>> e = Q(d) & Q(e=5)
>>> f = e | ~Q(f=6)
>>> repr(f) # actually just typing f<enter> into ipython/pdb/python REPL
'<django.db.models.query_utils.Q at 0x103184050>'
}}}
That's not exactly useful, given a Q object may represent a complex tree.
As it turns out, Q objects '''do''' implement str, which is the SQL output
for the Node subclass, which would be more useful than the above repr, and
possibly more useful than the nested repr of .children

The simplest repr implementation would probably be something like:

{{{
def __repr__(self):
return '{obj.__class__!r} {obj!s}'.format(obj=self)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22531>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 28, 2014, 7:46:59 AM4/28/14
to django-...@googlegroups.com
#22531: Q objects lack a useful repr
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
<django@…> | Status: new
Type: New feature | Version: master
Component: Database layer | Resolution:
(models, ORM) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: | Patch needs improvement: 0
Has patch: 0 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/22531#comment:1>

Django

unread,
May 3, 2014, 11:52:52 AM5/3/14
to django-...@googlegroups.com
#22531: Q objects lack a useful repr
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
<django@…> | Status: new
Type: New feature | Version: master
Component: Database layer | Resolution:
(models, ORM) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: | Patch needs improvement: 0
Has patch: 0 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by mardini):

PR: https://github.com/django/django/pull/2628

--
Ticket URL: <https://code.djangoproject.com/ticket/22531#comment:2>

Django

unread,
May 16, 2014, 9:31:26 AM5/16/14
to django-...@googlegroups.com
#22531: Q objects lack a useful repr
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
<django@…> | Status: closed

Type: New feature | Version: master
Component: Database layer | Resolution: fixed

(models, ORM) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: | Patch needs improvement: 0
Has patch: 0 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"393ddc10a78354b573b9e746247df013f7bd6151"]:
{{{
#!CommitTicketReference repository=""
revision="393ddc10a78354b573b9e746247df013f7bd6151"
Fixed #22531 -- Added tree.Node.__repr__ and tests for the class.

While Node class has a useful `__str__`, its `__repr__` is not that
useful. Added a `__repr__` that makes use of the current `__str__`.
This is especially useful since the more popular `Q` class inherits
`tree.Node`. Also created new tests that cover most of `Node` class
functionality.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22531#comment:3>

Reply all
Reply to author
Forward
0 new messages