Changes between Initial Version and Version 1 of Ticket #23533, comment 4


Ignore:
Timestamp:
Apr 6, 2024, 2:51:05 PM (7 months ago)
Author:
Simon Charette

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #23533, comment 4

    initial v1  
    33Approaches 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.
    44
    5 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
     5In 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?
     6
     7Some code example might help understanding better here.
     8
     9Today the ''problem'' is
     10
     11{{{#!python
     12class FooQueryset(models.QuerySet):
     13    def is_bar(self):
     14        return self.filter(bar=True)
     15
     16class FooBazManager(FooQueryset.as_manager()):
     17    def get_queryset(self):
     18        return super().get_queryset().filter(baz=True)
     19
     20class FooBatQueryset(FooQueryset.as_manager()):
     21    ddef get_queryset(self):
     22        return super().get_queryset().filter(baz=True)
     23
     24class Foo(models.Model):
     25    bar = models.BooleanField()
     26    baz = models.BooleanField()
     27    bat = models.BooleanField()
     28
     29    objects = FooQueryset.as_manager()
     30    baz_objects = FooBazQueryset.as_manager()
     31    bat_objects = FooBatQueryset.as_manager()
     32}}}
     33
     34What `get_initial_queryset` would allow
    635
    736{{{#!python
     
    2857}}}
    2958
    30 Compare that with
     59And what a more thorough solution would provide
    3160
    3261{{{#!python
     
    4473    bat_objects = FooQueryset.filter(bat=True).as_manager()
    4574}}}
     75
     76I don't believe that `get_initial_queryset` brings much to the table TBH.
Back to Top