On Mon, May 16, 2011 at 11:54:14PM +0200, Marc Aymerich wrote:
> > > 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)
> 
> Hi Michal,
> Yep, this is actually what I want to do, pass a Q() object to the
> active_during() method. My question is, how the active_during method should
> be in order to work with a Q() object passed as a parameter?
> 
> sorry if I wasn't clear in my firts mail.
Eh, now I probably see what you're trying to do. Well, I don't say it
is impossible, however, here you're trying to misuse Q objects in a
way they are not supposed to work.

A Q object is intended to represent a primitive filter on a model
field or a composition of such filters. If I understand right, you're
trying to make a single Q object represent a composition of primitive
filters.

I see two options here:
1) modify the active_during method to accept a list of tuples where
   each tuple would be (ini, end) and then build a disjunction of such
   individual filters
2) modify active_during to return the constructed Q object instead of
   adding it to the QuerySet directly and then add their disjunction
   to the QuerySet like this:

   Model.objects.filter(Model.objects.active_during(start1, end1) |
                        Model.objects.active_during(start2, end2))

   In this case, however, making it a manager method would be pretty
   much pointless.

Of course, you can always try to parse the Q object structure inside
active_during to make your example work but I'd strongly advise
against it, that would be way more difficult and dependent on the
internal implementation of the Q class.

Michal

Attachment: signature.asc
Description: Digital signature

Reply via email to