views:

13

answers:

3
def by_this(self):
        return super(MyModelManager, self).get_query_set().filter(this=True)

def by_that(self):
        return super(MyModelManager, self).get_query_set().filter(that=True)

If i do MyModel.objects.by_this() or by_that() it works.

But i want to do: MyModel.objects.by_this().by_that()

A: 

MyModel.objects will return your ModelManager type, but by_this returns a queryset. So you can't call by_that on the returned object and the chaining doesn't work. You could do: MyModel.objects.by_this().filter(that=True). Or just define a by_this_and_that method in your ModelManager derived class.

ars
A: 

As ars says, your methods return a queryset. So what you need to do is to create a custom subclass of QuerySet, which contains the by_this and by_that methods, and then in MyModelManager.get_query_set return your subclassed queryset.

Daniel Roseman
+1  A: 

As others say, you need custom QuerySet. Here are some examples of how to do this:

http://djangosnippets.org/snippets/562/
http://adam.gomaa.us/blog/2009/feb/16/subclassing-django-querysets/index.html

Tomasz Zielinski
Thank you, the custom QuerySet worked exactly how i wanted
Victor