I'm confused about the correct way to use Django custom model managers - based on the docs you can create a series of managers for one model as a way of filtering. But why not create one manager class with a series of functions for filtering? Is one method better than the other? and why?
For example:
class MaleManager(models.Manager):
def get_query_set(self):
return super(MaleManager, self).get_query_set().filter(sex='M')
class FemaleManager(models.Manager):
def get_query_set(self):
return super(FemaleManager, self).get_query_set().filter(sex='F')
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
sex = models.CharField(max_length=1, choices=(('M', 'Male'), ('F', 'Female')))
people = models.Manager()
men = MaleManager()
women = FemaleManager()
versus
class PersonManager(models.Manager):
def males(self):
return super(MaleManager, self).get_query_set().filter(sex='M')
def females(self):
return super(FemaleManager, self).get_query_set().filter(sex='F')
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
sex = models.CharField(max_length=1, choices=(('M', 'Male'), ('F', 'Female')))
people = PersonManager()
Person.people.females() # to get list of females
Person.people.males() # to get a list of males