What's the difference between a custom Manager and a custom QuerySet?
How Django model managers and custom querysets differ, and where a custom method belongs. Model.objects is a Manager instance attached to the class, deliberately inaccessible from an instance, and every row-returning method on it starts by calling getqueryset() to build a fresh QuerySet. That is why a method defined on a Manager can only ever be the first call in an expression: once filter() has returned a plain QuerySet, the Manager is out of the picture and the attribute no longer exists. A method defined on a QuerySet subclass composes in any position, and QuerySet.asmanager() or Manager.fromqueryset() exposes it on the manager too. Explains how overriding getqueryset() re-bases every method on that manager, making soft-delete filtering work and making get() raise DoesNotExist for rows that exist. Includes runnable Django code and real output, plus the default manager ordering trap, basemanagername, and losing the automatic objects attribute.
Комментарии