Through a custom manager you can change the behavior of django as a whole.
If you do:
class Foo(models.Model) :
bar = models.IntegerField('a number')
objects = MyFancyManager()
it is also used e.g. by the django admin
Your static method only works with your own code.
BTW: Your static method should probably use Foo.objects.filter(bar_gt=0)
AFAICT its only a matter of personal preference. Use which ever method
you like best.
Managers as a concept are very useful. For example, they can also
inherit from one another, which I use in a couple instances. A base
class might provide the initial queryset, which is further modified by
descendant managers: One returning only rows that are intended for the
general public, while another might allow access to everything,
including e.g. unapproved records.
With static methods, you also couldn't do things like:
if request.user.is_admin:
manager = MyModel.objects
else:
manager = MyModel.public_objects
# work with ``manager``
Michael
Well, there is a distinct philosophy behind the manager concept; we
didn't just pull the idea out of thin air :)
The basic idea is that manager act as the bridge between Python
classes and relational database tables. Managers, then, represent
database-table-wide operations -- not, say, row-level operations
(which are methods on model instances) or Python-level operations
(which don't know anything about a database at all).
For a concrete example, consider the difference in behavior between
``Model.__init__`` and ``Model.objects.create``. Both "create
something", but both do *very* different things.
``Model.__init__`` creates a new model instance, but doesn't touch the
database in any way -- it's just like any other Python constructor
that returns an instance. ``Model.objects.create()`` also returns a
new model instance, but *also* creates a new row in the database
table. Confuses those two method at your peril!
So the distinguishing factor between classmethods and manager methods
should be the database. In the original example here, ``positives()``
should be a manager method to keep in line with this philosophy.
That's not to say that there's anything "wrong" with making it
classmethod, but if you're trying to be as "django-esque" as possible,
you'd make that method a manager method.
Jacob
This only works in Python 2.5. The other works in every version of
Python Django supports.
--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."