On Wed, 2009-01-28 at 16:10 +0100, Stefan Tunsch wrote: > Hi! > > I have a calendar that permits multiple selections of dates. > > My view has to filter the data depending on the selected dates. > > I am unsure about how I can construct a query that filters correctly the > data. > > I construct my list of datetime objects like this: > > list = request.session['selected_dates'].split(',') > date_list = [datetime.datetime(int(mydate.split('/')[2]), > int(mydate.split('/')[0]), int(mydate.split('/')[1])) for mydate in list] > > > If I only have one date in my list, this works: > > qdates = Q(mydatetimefield__year=date_list[0].year, > mydatetimefield__month=date_list[0].month, > mydatetimefield__day=date_list[0].day)
An easier approach than this is to convert the input values to a Python datetime object and then filter using Q(mydatetimefield=input_datetime) The "__year", "__month" and "__day" lookups are forcing the database to make three comparisons on the same field, instead of one. It's also a lot less readable at the Python level. > myqueryset = myqueryset.filter(qdates) #I am modifying an existing queryset > > The problem stems of the need to assign new values to my Q object > dynamically. > > I have tried this, but it does not work. (It will always filter only to > the first selected date): > > qdates = Q() > for myfinaldate in date_list: > qdates |= Q(**{"mydatetimefield__year": myfinaldateyear}) > qdates &= Q(**{"mydatetimefield__month": myfinaldate.month}) > qdates &= Q(**{"mydatetimefield__day": myfinaldate.day}) > > > First I would want to know if there is a better way to lookup data that > is IN a range of data? Since you're not using "IN" here at all, this doesn't seem to relate to the code you're doing. I have been wondering why you're not using the "__in" lookup type here. If you have a list of Python datetime objects (and not doing that is just doing things the hard way), then Q(mydatetimefield__in = [...]) if the easiest way to do this. Construct the list of datetimes objects before constructing the filter. > Second I would want to know if I can somehow add to my Q object a mix of > groups of ANDed values that are ORed between them? Yes. But you have to add them as parenthesised groups. Your above code seems to be making some kind of assumptions about the precedence of "|" and "&" and I can't work out what it's doing. However, the straightforward (Q(..) | Q(...)) & (Q(...) | Q(...) | Q(...)) sort of style works. You could construct the pieces (the "|" bits in the above example) and then "&" them together at the end, for example. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---