Applying custom manager methods on querysets

45 views
Skip to first unread message

cesco

unread,
Jul 4, 2007, 3:15:11 PM7/4/07
to Django users
Hi,

in django we can chain multiple filter methods like:

MyModel.objects.filter(field1__exact=value1).filter(field2__exact=value2)

but I'd like to be able to chain custom filter methods defined by
extending the models.Manager class.
For example:

MyModel.objects.filter(field1__exact=value1).my_filter(field2)

In other words I'd like to be able to apply the filter directly to a
queryset and not only to MyModel.objects.

Do you have any suggestion on how to accomplish that?

Thanks a lot
Francesco

Malcolm Tredinnick

unread,
Jul 4, 2007, 9:08:48 PM7/4/07
to django...@googlegroups.com

Sub-class the QuerySet class and have your model's manager return your
subclass as the result of get_query_set() rather than the current
QuerySet instance that is returned.

Look in django/db/models/manager.py for the default manager behaviour
and write something similar for get_query_set().

Regards,
Malcolm

--
Tolkien is hobbit-forming.
http://www.pointy-stick.com/blog/

Reply all
Reply to author
Forward
0 new messages