[Django] #30631: Prefixing Q Objects

26 views
Skip to first unread message

Django

unread,
Jul 10, 2019, 1:07:17 PM7/10/19
to django-...@googlegroups.com
#30631: Prefixing Q Objects
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
efficiosoft |
Type: New | Status: new
feature |
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords: prefix q objects
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I'm currently spending a lot of time on development of a project using
Django and worked out something that I think could be of use for all
Django users.

I added a new `.prefix(prefix)` method to the `Q` object, allowing to
shift pre-built `q` objects to a related field. I think this can change
the way people build managers completely, because instead of methods
returning filtered querysets, one can just return `Q` objects, which can
then be used from related models without repeating the filtering logic.

This is the simple implementation.

```
class Q(django.db.models.Q):
"""
A custom Q implementation that allows prefixing existing Q objects
with some
related field name dynamically.
"""

def prefix(self, prefix):
"""Recursively copies the Q object, prefixing all lookup keys.

The prefix and the existing filter key are delimited by the lookup
separator __.
Use this feature to delegate existing query constraints to a
related field.
"""
return type(self)(
*(
child.prefix(prefix)
if isinstance(child, Q)
else (prefix + LOOKUP_SEP + child[0], child[1])
for child in self.children
),
_connector=self.connector,
_negated=self.negated,
)
```

What do you think, is it worth creating a PR for this functionality? I
haven't written the docs yet, but could write something if you like the
addition.

Best regards
Robert

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

Django

unread,
Jul 10, 2019, 1:13:05 PM7/10/19
to django-...@googlegroups.com
#30631: Prefixing Q Objects
-------------------------------------+-------------------------------------
Reporter: efficiosoft | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:

Keywords: prefix q objects | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by efficiosoft):

* needs_docs: 0 => 1
* needs_tests: 0 => 1


Old description:

New description:

Best regards
Robert

--

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

Django

unread,
Jul 10, 2019, 1:49:58 PM7/10/19
to django-...@googlegroups.com
#30631: Prefixing Q Objects.

-------------------------------------+-------------------------------------
Reporter: efficiosoft | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: prefix q objects | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* version: 2.2 => master
* resolution: => wontfix


Comment:

Thanks for this proposition. It is interesting idea but I don't see many
use cases and to be honest it would be confusing to me to use, e.g.:
{{{
Q(Q(field__startswith='x') |
Q(other_field__startswith='y')).prefix('related_field') &
Q(base_field__gte=78)
}}}
instead of
{{{
Q(Q(related_field__field__startswith='x') |
Q(related_field__other_field__startswith='y')) & Q(base_field__gte=78)
}}}

Please write to the DevelopersMailingList if you want other opinions, we
can re-open this ticket if we reach a consensus on the mailing list.

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

Django

unread,
Jul 10, 2019, 3:18:22 PM7/10/19
to django-...@googlegroups.com
#30631: Prefixing Q Objects.
-------------------------------------+-------------------------------------
Reporter: efficiosoft | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: prefix q objects | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by efficiosoft):

Ok, I posted to the mailing list, but I think you've misunderstood the use
case I proposed. Let's see how it goes.

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

Django

unread,
May 1, 2020, 12:24:47 PM5/1/20
to django-...@googlegroups.com
#30631: Prefixing Q Objects.
-------------------------------------+-------------------------------------
Reporter: Robert Schindler | Owner: nobody

Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: prefix q objects | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by valentijnscholten):

Discussion continued at mailing list: https://groups.google.com/d/msg
/django-developers/jEkCdzGnzRE/kXgYt7r0CgAJ

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

Reply all
Reply to author
Forward
0 new messages