Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subclassing models.Manager

I see no difference in sub classing the models.manager object and overriding the get_query_set method or simply creating a new method in the sub class and using the method. For the reason being I have taken example from the django book;

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()

With this I could use; Person.women.all() or Person.men.all() to fetch all the men or women model object. But, I guess the similar thing can be achieved without overriding the get_query_set method by simply doing;

class MaleManager(models.Manager):
    def get_male(self):
        return self.filter(sex='M')
class FemaleManager(models.Manager):
    def get_female(self):
        return  return self.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()

Now, with this, I can fetch all those objects with a little technique like; Person.objects.get_male() or Person.objects.get_female(). But, there's no any subtle difference between how I could fetch the objects yet there is difference in terms of readability and use in first case while the second one is much easier to understand and has lesser code.Do they make some significant difference in coding and patterns ? The other thing with the second one, what if I place both the methods inside a same class like;

class PeopleManager(models.Manager):
        def get_male(self):
            return self.filter(sex='M')
        def get_female(self):
            return  return self.filter(sex='F')
like image 359
Sandeep Avatar asked Dec 27 '11 03:12

Sandeep


1 Answers

  1. Usually you don't want several managers for the model. It's better to extend default manager.

    class PeopleManager(models.Manager):
        def get_male(self):
            return self.filter(sex='M')
    
        def get_female(self):
            return  return self.filter(sex='F')
    
    class Person(models.Model):
        ....
        objects = PeopleManager()
    

    Then you will be able to use Person.objects.get_male(), Person.objects.get_female() and built-in methods like Person.objects.order_by(). You can look at custom managers in django.contrib.auth.models for example.

  2. get_query_set is good for inheritance. For example you can define

    class SmithManager(PeopleManager):
        def get_queryset(self):
            return super(SmithManager, self).get_query_set().filter(last_name='Smith')
    

    and all the methods of the manager will return only Smiths, (Person.objects.get_male() will return only males named Smith and so on). And you don't need to rewrite all the methods.

like image 138
DrTyrsa Avatar answered Nov 12 '22 01:11

DrTyrsa