Задать вопрос

Тел: +7 965 3737 888

516

Просмотров

3

Ответов

QuerySetManager - easily add new QuerySet methods using a Model inner class

An easy way to add custom methods to the QuerySet used by a Django model. See simonwillison.net/2008/May/1/orm/ for an in-depth explanation.

Вопрос полезен? Да0/Нет0
file_3613.py(667.0б)
None

Ответы (3):

Ответzodiac2832:28.07.2011
Ответ полезен? Да0/Нет0

I had to modify my manager to the following so I would not receive Pickle Errors.

# pickling causes recursion errors
_deny_methods = ['__getstate__', '__setstate__', '_db']

def __init__(self, queryset_cls=None):
    self._queryset_cls = queryset_cls
    super(PadManager, self).__init__()

def __getattr__(self, name):
    if name in self._deny_methods:
        raise AttributeError(name)
    return getattr(self.get_query_set(), name)

def get_query_set(self):
    return PadQuerySet(self.model)

Ответexogen:08.05.2008
Ответ полезен? Да0/Нет0

In response to the above comment, I believe it can simple be written as:

def __getattr__(self, name):
    return getattr(self.get_query_set(), name)

...since the default attribute lookup will check self.class and superclasses.

Ответdnordberg:06.05.2008
Ответ полезен? Да0/Нет0

This works for Entry.objects.filter().on_date(date) but not Entry.objects.on_date(date) as some may wish, for this add the following snippet to QuerySetManager.

`

def __getattr__(self, attr, *args):
    try:
        return getattr(self.__class__, attr, *args)
    except AttributeError:
        return getattr(self.get_query_set(), attr, *args)

`