[Fixed]-Django custom model managers


With first method you can write:

men = Person.men.all()


peters_men = Person.men.filter(first_name='Peter')

For the second one method get_query_set is ‘inherit’ from model, then it return query set without customization. I don’t know any reason to discard your second method, if you are using Admin interface you should check that this is suported.

Also, for the second one method you should correct your question. Is

class PersonManager(models.Manager):
    def males(self):
        return super(PersonManager, self).get_query_set().filter(sex='M')

Read django manager doc: “You can override a Manager’s base QuerySet by overriding the Manager.get_query_set() method. get_query_set() should return a QuerySet with the properties you require.”

Edited 2017 Be careful, get_query_set is renamed from djanto 1.7 to get_queryset. More info at Modifying a manager’s initial QuerySet

Leave a comment