Django, which function belongs to QuerySet and Manager?

╄→гoц情女王★ 提交于 2019-12-12 03:24:46

问题


I used to think QuerySet method return QuerySet instances, but it apparently not.

For instance, count() is a queryset method not manager's

How do I decide which functions go to custom QuerySet and which go to custom Manager class?


回答1:


It makes sense to be able to access some functions like count() on the manager and the queryset. This allows you to do:

Blog.objects.count()  # total number of blogs
Blog.objects.filter(status='PUBLISHED').count()  # Number of published blogs

Django has a method as_manager which allows you to create a manager from a custom queryset. This means you don't have to duplicate your methods on the manager and queryset.



来源:https://stackoverflow.com/questions/37239924/django-which-function-belongs-to-queryset-and-manager

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