问题
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