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.
  • get... Ramez Ashraf
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
      • ... Ramez Ashraf
        • ... Shai Berger
          • ... Ramez Ashraf

Reply via email to