Opened 9 years ago

Last modified 9 years ago

#23533 new New feature

Hook for default QuerySet filtering defined on the QuerySet itself.

Reported by: Loic Bistuer 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


Django 1.7 brought managers automatically created from QuerySet which replaces defining a custom manager for the purpose of defining reusable methods. Refs #20625.

One use-case remains inelegant: using a custom QuerySet with default QuerySet customization/filtering:

BaseCustomManager = Manager.from_queryset(CustomQueryset)

class CustomManager (BaseCustomManager ):
    def get_queryset(self):
        queryset = super(Manager, self).get_queryset()
        return queryset.filter(...)

This ticket proposes adding a hook on QuerySet to enable this without requiring a custom Manager.

Change History (2)

comment:1 Changed 9 years ago by Loic Bistuer

It's worth noting that QuerySet.__init__() can't be used for providing such initialization/customization:

  • QuerySet are often cloned, and the customization should only ever apply once.
  • QuerySet methods return a cloned QuerySet instance and __init__ can't return a different instance.

So far the best option I can think of is a hook called externally by the manager.

POC with a QuerySet.get_initial_queryset() method

comment:2 Changed 9 years ago by Tim Graham

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