Opened 11 years ago
Closed 10 years ago
#22531 closed New feature (fixed)
Q objects lack a useful repr
Reported by: | 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 , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 11 years ago
comment:3 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
PR: https://github.com/django/django/pull/2628