Opened 11 years ago

Closed 11 years ago

#22531 closed New feature (fixed)

Q objects lack a useful repr

Reported by: Keryn Knight <django@…> Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (3)

comment:1 by Tim Graham, 11 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 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.

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