Take the 2-minute tour ×

I'm trying to create a custom function that I can place in a queryset "chain" that will apply a filter to the queryset. Like with normal Django queryset filters, it will take the queryset to the left of it and pass the resulting queryset to the right.

Before adding my custom function to the queryset chain:


After adding my custom function to the queryset chain:


I'm not sure how to construct my function to do this. Does it need some sort of decorator around it?

???? def my_custom_filter(???):
????    return qs.filter(id__gte=10)  

Does anyone know a good way to accomplish this?

The following might work, but I was hoping for something a little more Django-like.

def my_custom_filter(qs):
    return qs.filter(id__gte=1)


Any advice is much appreciated.

Thanks, Joe

UPDATE: I'm trying to work out the details of Ignacio's solution. I've not done too much with QuerySet overriding so I'm piecing together what I'm able to find...

class MyQuerySet(QuerySet):

        return super(self.__class__, self).filter(*args, **kwargs).\

class MyManager(models.Manager):

    def testqs(self):
        return MyQuerySet(self.model)

However, I don't think this is working the way I expect. Any suggestions?

>>> models.MyModel.objects.testqs().filter()

UPDATE 2: This article proved to be useful. http://zmsmith.com/2010/04/using-custom-django-querysets/

share|improve this question
+1 for the link in update2, helped me too. –  Marcio Cruz Feb 13 '12 at 18:09
add comment

1 Answer

up vote 3 down vote accepted

You will need to write your own QuerySet child class with the method added, then use that class in the manager.

share|improve this answer
Thank you for your advice Ignacio. I've made updates above. –  Joe J Feb 8 '11 at 23:35
add comment

Your Answer


