Code

Opened 5 years ago

Last modified 4 years ago

#11981 new New feature

Add custom Q-object (add_to_query) support to QuerySet.filter() and QuerySet.exclude()

Reported by: emulbreh Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: complex_filter, add_to_query
Cc: Triage Stage: Someday/Maybe
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It would be nice if custom Q-objects could be transparently used in filter() and exclude() calls (if they provide an __invert__() method).

The following code illustrates the desired behaviour:

class QuerySet(models.query.QuerySet):
    def filter(self, *args, **kwargs):
        if not kwargs and len(args) == 1 and hasattr(args[0], 'add_to_query'):
            return self.complex_filter(args[0])
        return super(QuerySet, self).filter(*args, **kwargs)

    def exclude(self, *args, **kwargs):
        if not kwargs and len(args) == 1:
            return self.complex_filter(~args[0])
        return super(QuerySet, self).exclude(*args, **kwargs)

Attachments (1)

11981.q_filter_exclude.diff (1.9 KB) - added by emulbreh 5 years ago.

Download all attachments as: .zip

Change History (8)

Changed 5 years ago by emulbreh

comment:1 Changed 5 years ago by anonymous

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by anonymous

  • Has patch unset
  • Version changed from 1.1 to SVN

comment:3 Changed 5 years ago by emulbreh

  • Has patch set

...

comment:4 Changed 4 years ago by russellm

  • Triage Stage changed from Unreviewed to Someday/Maybe

In order to do this, we would need to formalize the interface that Q objects expose. I'm not sure we're in a position to do that at the moment. Long term, it's certainly a desirable goal, though.

comment:5 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:6 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:7 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.