Django custom model managers

左心房为你撑大大i 提交于 2019-12-02 20:59:12

With first method you can write:

men = Person.men.all()

or

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!