On Sun, 1 Jan 2023 16:33:45 +0200
Ramez Ashraf <ramezash...@gmail.com> wrote:

> 
> Interested or do you think the function can be enhanced it to make
> more useable for your everyday other cases ?
> 

This is half-baked, just a thought, but maybe you can take it some
place interesting:

Imagine a class that "collects" calls for later execution. Something
like (this exactly won't work, details below):

from functools import partialmethod
from django.db.models import QuerySet as QSet

class QS:
        def __init__(self):
                self._calls = []
        def filter(self, *args, **kw):
                self._calls.append(
                        partialmethod(QSet.filter, *args, **kw)
                ) 
                return self
        def annotate(*args, **kw):
                # ... (same idea)
        # ... other relevant methods, 
        def __call__(self, qset):
                for call in self._calls:
                        qset = apply(call, qset)
                return qset

This won't work, I think, because partialmethod isn't supposed to work
quite this way, and the "apply" in the last line isn't defined. But
with this (fixed) already you could, I think, do something like

def qset_manager(qs: QS) -> Manager
        class CustomManager(models.Manager):
                def get_queryset(self):
                        orig = super().get_queryset()
                        return qs(orig)
        return CustomManager()

And then, in your model, 

class Person(models.Model):
        ...
        authors = qset_manager(QS().filter(role="A"))

and now the "make a manager with a modified queryset" pattern is
shortened in a general way.

As I said, just a half-baked thought. There's problems here to solve,
and many improvements to make.

HTH,
        Shai.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/20230101190053.671e8303.shai%40platonix.com.
  • get... Ramez Ashraf
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
      • ... Ramez Ashraf
        • ... Shai Berger
          • ... Ramez Ashraf

Reply via email to