On Mon, May 16, 2011 at 8:53 PM, Shawn Milochik <sh...@milochik.com> wrote:

> On 05/16/2011 02:48 PM, Marc Aymerich wrote:
>
>> Hi,
>> my question is, How can I define a custom manager that can handle Q
>> objects?
>>
>> Thanks!
>>
> A custom manager, being a subclass of models.Manager, can already handle Q
> objects in the built-in methods, such as filter and get.
>
> What error are you getting?
>

Hi Swawm.
Thanks for your answer.

when I call Model.objects.active_during(Q(Q(ini=some_date, end=some_date) |
Q(ini=other_date, fin=other_date))) I get This error:


active_during() takes exactly 3 arguments (2 given)



this is my code:

class OrderQuerySet(models.query.QuerySet):
    def active_during(self, ini, end):
        return self.filter(Q(register_date__lte = end) &
                    Q(Q(cancel_date__isnull=True) | Q(cancel_date__gte = ini
)))

class OrderManager(models.Manager):
    # Custom managers with chainable filters.
    # Based on: http://djangosnippets.org/snippets/562/

    def __init__(self, qs_class=models.query.QuerySet):
        super(OrderManager,self).__init__()
        self.queryset_class = qs_class

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

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


Seems to me that I need to define active_during() like:
def active_during(self, *args, **kwargs)

and make some custom stuff if a Q() object is passed to the method. But what
I need to do?

-- 
Marc

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to