Skip to content

Instantly share code, notes, and snippets.

@brehaut
Created September 25, 2012 02:41
Show Gist options
  • Save brehaut/3779678 to your computer and use it in GitHub Desktop.
Save brehaut/3779678 to your computer and use it in GitHub Desktop.
A django manager that takes a function to filter a queryset
from django.db import models
class LambdaManager(models.Manager):
"""LambdaManager is a simple manager extension that is instantiated with a callable.
This callable is passed the query set by get_query_set so that it can perform any
additional transformations to it such as eg filtering.
"""
def __init__(self, f):
super(LambdaManager, self).__init__()
self.transform = f
def get_query_set(self):
return self.transform(super(LambdaManager, self).get_query_set())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment