#32549: Add `Q.empty()` to check for nested empty Q objects like `Q(Q())` -------------------------------------+------------------------------------- Reporter: jonathan-golorry | Owner: nobody Type: | Status: closed Cleanup/optimization | Component: Database layer | Version: dev (models, ORM) | Severity: Normal | Resolution: wontfix Keywords: Q objects, nested, | Triage Stage: empty | Unreviewed Has patch: 1 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by jonathan-golorry): We already shortcut logical operations between empty Q objects, so I view this more as an extension of that shortcutting (for legibility, not performance). I don't think it's fair to say it's backwards incompatible, since the simplified Q objects will be logically equivalent to the old ones. The expected outcome of Q object combinations is logical consistency, not a specific tree structure (as evidenced by `len((Q(x=1, y=2) | Q(x=1, y=2)).children == 1)`. My original example shows why it's desirable to have `q.empty()` behave differently from `not bool(q)`. Empty Q objects are a potential security risk, but the standard way to check for them doesn't catch nested empty Q objects. #32554 doesn't actually build nested queries, it just uses this to avoid bugs with nested query inputs. My apologies for reopening without a mailing list consensus, though I feel "If there is a way they can improve the ticket to reopen it, let them know" indicates that I may reopen if I address the only concern you expressed in your closing comment. If the reason for closing is that legibility of Q objects isn't it's own concern, I can roll `empty` into #32554 without the logic shortcut. -- Ticket URL: <https://code.djangoproject.com/ticket/32549#comment:5> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/074.4b3ee5e771602e6fb6f80bf352e89d6f%40djangoproject.com.