A zero-boilerplate generalisation of http://djangosnippets.org/snippets/2114/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | from types import ClassType
from django.db.models.manager import Manager
from django.db.models.query import QuerySet
def manager_from(*mixins, **kwds):
'''
Returns a Manager instance with extra methods, also available and
chainable on generated querysets.
:param mixins: Each ``mixin`` can be either a class or a function. The
generated manager and associated queryset subclasses extend the mixin
classes and include the mixin functions (as methods).
:keyword queryset_cls: The base queryset class to extend from
(``django.db.models.query.QuerySet`` by default).
:keyword manager_cls: The base manager class to extend from
(``django.db.models.manager.Manager`` by default).
'''
# collect separately the mixin classes and methods
bases = [kwds.get('queryset_cls', QuerySet)]
methods = {}
for mixin in mixins:
if isinstance(mixin, (ClassType, type)):
bases.append(mixin)
else:
try: methods[mixin.__name__] = mixin
except AttributeError:
raise TypeError('Mixin must be class or function, not %s' %
mixin.__class__)
# create the QuerySet subclass
id = hash(mixins + tuple(kwds.iteritems()))
new_queryset_cls = type('Queryset_%d' % id, tuple(bases), methods)
# create the Manager subclass
bases[0] = manager_cls = kwds.get('manager_cls', Manager)
new_manager_cls = type('Manager_%d' % id, tuple(bases), methods)
# and finally override new manager's get_query_set
super_get_query_set = manager_cls.get_query_set
def get_query_set(self):
# first honor the super manager's get_query_set
qs = super_get_query_set(self)
# and then try to bless the returned queryset by reassigning it to the
# newly created Queryset class, though this may not be feasible
if not issubclass(new_queryset_cls, qs.__class__):
raise TypeError('QuerySet subclass conflict: cannot determine a '
'unique class for queryset instance')
qs.__class__ = new_queryset_cls
return qs
new_manager_cls.get_query_set = get_query_set
return new_manager_cls()
#==== example ==================================================================
from datetime import datetime
from django.db import models
from django.contrib.auth.models import User
class AuthorMixin(object):
def by_author(self, user):
return self.filter(user=user)
class PublishedMixin(object):
def published(self):
return self.filter(published__lte=datetime.now())
def unpublished(self):
return self.filter(published__gte=datetime.now())
class CustomManager(Manager):
def get_query_set(self):
return super(CustomManager, self).get_query_set().order_by('-published')
class Post(models.Model):
user = models.ForeignKey(User)
published = models.DateTimeField()
objects = manager_from(AuthorMixin, PublishedMixin, unpublished,
manager_cls=CustomManager)
print Post.objects.by_author(user=12).unpublished().query
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
@carljm, no it's not released anywhere; feel free to include it in your project with proper attribution.
#
2010-07-22: Fixed bug that was effectively ignoring the
get_query_set
of a passed custom manager. Now the parentget_query_set
is honored, unless there is a conflict between the type of the returned queryset and the dynamically generated QuerySet subclass, in which case a TypeError is raised.#
Indentation of lines 67 & 68 needs a tab to the right?
#
Sorry, please ignore my comment above.
It's clever how you can pass in a function or a class :-)
#
Please login first before commenting.