Your proposal is quite niche: it only shortens the creation of a Manager
class when you need to add a single filter() call. The function seems like
it might be a useful helper within a project, if you use many such
managers. But custom managers might use any queryset methods, or other
logic. I've often used a custom manager to add annotate() and
prefetch_related() calls.

So, I think your shortcut is a smart addition for your project, but perhaps
not suitable for Django itself. I think it's worth sharing in a blog post
though!

On Mon, Dec 19, 2022 at 10:51 PM Ramez Ashraf <ramezash...@gmail.com> wrote:

> Hello everyone,
>
> I want to propose a helper function
> `get_manager(parent_manager=None, *args, **kwargs)`
>
> A shortcut to return a manager, *args & **kwargs are passed to the manager
> .filter()
> pass parent_manager to customize the parent manager, defaults to
> ``Manager``.
>
> it can be used like this
>
> from django.db.models import get_manager
>
> class Person(models.Model):
>     # ...
>     authors = models.get_manager(role='A')
>     editors = models.get_manager(role='E')
>     special = get_manager(~Q(name="Roald Dahl"), role='E')
>     manager_with_counts = get_manager(parent_manager=PollManager,
> name="Roald Dahl")
>
> Instead of the current
>
> class AuthorManager(models.Manager):
>     def get_queryset(self):
>         return super().get_queryset().filter(role='A')
>
> class EditorManager(models.Manager):
>     def get_queryset(self):
>         return super().get_queryset().filter(role='E')
>
> class Person(models.Model):
>     people = models.Manager()
>     authors = AuthorManager()
>     editors = EditorManager()
>    ...
>
> The first is more readable and nicer .. yeah ?
> Code is working and ready at  github
> <https://github.com/django/django/compare/main...RamezIssac:django:feature/get_manager_shortcut>
>  ,
>
> Do i get +1(s) to move forward and create a ticket ?
>
>
> --
> 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/f0e4367d-8a7e-48da-b78c-35036015212fn%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/f0e4367d-8a7e-48da-b78c-35036015212fn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAMyDDM0Jk%3DYsXQVRydaanmg%3DP_wMMmg%2B4uPETSvEXs3jFgCB%2BQ%40mail.gmail.com.
  • get... Ramez Ashraf
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
      • ... Ramez Ashraf
        • ... Shai Berger
          • ... Ramez Ashraf

Reply via email to