4 ms·
There's a couple reasons that's not ideal. The big one is that you'd lose filter chaining. With your example you couldn't do Todo.objects.high_priority().
by lojack 14y ago
There's a couple reasons that's not ideal.
The big one is that you'd lose filter chaining. With your example you couldn't do
Todo.objects.high_priority().incomplete()
The other issue is a semantic one. In your example you could do:
Todo.objects.all()[0].incomplete()
which will return a QuerySet of all incomplete Todo items. This, at least to me, doesn't make sense.
The last reason is that by using a Manager you are encapsulating this filter data. If you later decide that you want to create a new model with similar types of filters, then you'd have to rewrite these methods. With a Manager, both models can simply use the same manager.
- jpwagner 14y agoNo, I believe you've read the code above incorrectly. This would be, for example: Todo.high_priority().all() and would allow, for example: Todo.high_priority().filter(id__gte=1) I haven't tested chaining these, but this might work: Todo.high_priority().incomplete().filter(id__gte=1)
- j4mie 14y agoYour last example wouldn't work. Todo.high_priority() returns a plain QuerySet, which won't have your "incomplete" method (as that's defined on the Model class in your example).
- lojack 14y agoSorry, my example was explaining how it would be done if you were using Managers. Using your example: Todo.high_priority().incomplete() would fail because the QuerySet doesn't have an incomplete() method.