Querysets in fc

position ease box interval due
front 2.95 5 51.57 2021-10-21T21:35:48Z

Custom querysets are configured by the model manager

Calling custom queryset method from manager

class PersonQuerySet(models.QuerySet):
    def authors(self):
        return self.filter(role='A')

    def editors(self):
        return self.filter(role='E')

class PersonManager(models.Manager):
    def get_queryset(self):
        return PersonQuerySet(self.model, using=self._db)

    def authors(self): # verbose
        return self.get_queryset().authors()

    def editors(self): # verbose
        return self.get_queryset().editors()

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    role = models.CharField(max_length=1, choices=[('A', _('Author')), ('E', _('Editor'))])
    people = PersonManager()

Creating manager from queryset method

It is shortcut for the above verbose method. Instead of creating both manager and queryset classes, you will just need a queryset class.

class Person(models.Model):
    ...
    people = PersonQuerySet.as_manager()