|
Well, first of all I can't believe I've overlooked the __in field
lookup... I wasn't aware of it's existance. Sorry. Regarding the __year, __month and __day lookups I'm using in my code: I do it because I want to filter all datetime objects of a specific day. I want to filter all datetime objects like datetime.datetime(2009, 1, 29, 12, 33, 41, 234000) that are in a day like datetime.date(2009, 1, 29). I would want to do something like mydatetimefield__date=datetime.date(2009, 1, 29) but since that option does not exist, I am doing this strange thing with year, month and day. I could also simply split my datetime field into two fields. A date field and a time field. This way I would be able to filter out objects that are in a list of dates, with the __in lookup and things would work very smoothly. Regarding the Q objects, the problem strives of wanting to create the Q object dynamically. The way I've been trying to do this is with a dict expansion as Tim Chase pointed out in this post: http://groups.google.es/group/django-users/browse_thread/thread/325752f0a113b5ef/ed1a2fc7eb6e6e54?hl=es#ed1a2fc7eb6e6e54 The problem with this approach is that I seem only to be able to add simple Q objects one by one. In the same post Sebastian Bauer points out a different approach which actually works well in my scenario here. The following code does it's job as it should: qdates = None for myfinaldate in date_list: if qdates: qdates= qdates | (Q(mydatetimefield__day=myfinaldate.day) & Q(mydatetimefield__month=myfinaldatemonth) & Q(mydatetimefield__year=myfinaldate.year)) else: qfechas = Q(mydatetimefield__day=myfinaldate.day) & Q(mydatetimefield__month=myfinaldate.month) & Q(mydatetimefield__year=myfinaldate.year) myqueryset = myqueryset.filter(qdates) Regards, Stefan Malcolm Tredinnick escribió: On Wed, 2009-01-28 at 16:10 +0100, Stefan Tunsch wrote:Hi! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~--- |
- Complex queries with datetime field Stefan Tunsch
- Re: Complex queries with datetime field Malcolm Tredinnick
- Re: Complex queries with datetime field Stefan Tunsch
- Re: Complex queries with datetime field Malcolm Tredinnick
- Re: Complex queries with datetime fiel... Stefan Tunsch

