#23533: Hook for default QuerySet filtering defined on the QuerySet itself.
-------------------------------------+-------------------------------------
Reporter: Loic Bistuer | Owner: Mariusz
| Felisiak
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):
It'd be good to explore other implementations than that
`get_initial_queryset` hook as that bi-directionnaly couples Querysets
with managers (even more than the existing `as_manager` method) and
prevents reuse of the same queryset class for different managers of the
same model.
Approaches such as `CustomQueryset.as_manager(filter=Q(is_active=True))`
and `CustomQueryset.filter(is_active=True).as_manager()` (this would
require marking some methods `class_or_instance_method` at
`__init_subclass__` time to capture the calls and apply them at
`Manager.contribute_to_class` / app readyness time) seem more valuable as
they don't require overriding any methods.
In other words, the `get_initial_queryset` hook saves you from defining a
manager but you still have to define a method. It also ties your queryset
class to a single manager usage with seems wrong? What if you want to use
your custom queryset class with two different filters sets
{{{#!python
class FooQueryset(models.QuerySet):
def is_bar(self):
return self.filter(bar=True)
class FooBazQueryset(FooQueryset):
def get_initial_queryset(self):
return self.filter(baz=True)
class FooBatQueryset(FooQueryset):
def get_initial_queryset(self):
return self.filter(bat=True)
class Foo(models.Model):
bar = models.BooleanField()
baz = models.BooleanField()
bat = models.BooleanField()
objects = FooQueryset.as_manager()
baz_objects = FooBazQueryset.as_manager()
bat_objects = FooBatQueryset.as_manager()
}}}
Compare that with
{{{#!python
class FooQueryset(models.QuerySet):
def is_bar(self):
return self.filter(bar=True)
class Foo(models.Model):
bar = models.BooleanField()
baz = models.BooleanField()
bat = models.BooleanField()
objects = FooQueryset.as_manager()
baz_objects = FooQueryset.filter(baz=True).as_manager()
bat_objects = FooQueryset.filter(bat=True).as_manager()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23533#comment:4>
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018eb4f3b1f0-44d77d7a-9daf-44ac-9df4-b2c26448312a-000000%40eu-central-1.amazonses.com.