PageRenderTime 13ms CodeModel.GetById 7ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/polymorphic/manager.py

https://bitbucket.org/bconstantin/django_polymorphic/
Python | 36 lines | 15 code | 7 blank | 14 comment | 3 complexity | 0b8b55b7e230b9683f3567eb8dce2f39 MD5 | raw file
 1# -*- coding: utf-8 -*-
 2""" PolymorphicManager
 3    Please see README.rst or DOCS.rst or http://bserve.webhop.org/wiki/django_polymorphic
 4"""
 5
 6from django.db import models
 7from query import PolymorphicQuerySet
 8
 9class PolymorphicManager(models.Manager):
10    """
11    Manager for PolymorphicModel
12
13    Usually not explicitly needed, except if a custom manager or
14    a custom queryset class is to be used.
15    """
16    use_for_related_fields = True
17
18    def __init__(self, queryset_class=None, *args, **kwrags):
19        if not queryset_class: self.queryset_class = PolymorphicQuerySet
20        else: self.queryset_class = queryset_class
21        super(PolymorphicManager, self).__init__(*args, **kwrags)
22
23    def get_query_set(self):
24        return self.queryset_class(self.model)
25
26    # Proxy all unknown method calls to the queryset, so that its members are
27    # directly accessible as PolymorphicModel.objects.*
28    # The advantage of this method is that not yet known member functions of derived querysets will be proxied as well.
29    # We exclude any special functions (__) from this automatic proxying.
30    def __getattr__(self, name):
31        if name.startswith('__'): return super(PolymorphicManager, self).__getattr__(self, name)
32        return getattr(self.get_query_set(), name)
33
34    def __unicode__(self):
35        return self.__class__.__name__ + ' (PolymorphicManager) using ' + self.queryset_class.__name__
36