Created
September 25, 2012 02:41
-
-
Save brehaut/3779678 to your computer and use it in GitHub Desktop.
A django manager that takes a function to filter a queryset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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