Hi Malcolm! Well, as you say, the range lookup simplifies the code. I am using MySQL and adding a timedelta of 1 day to a date object and comparing the range with my datetime field works indeed as expected. Thanks for your help. It's been very instructive. ;) Regads, Stefan Malcolm Tredinnick escribió: On Thu, 2009-01-29 at 09:48 +0100, Stefan Tunsch wrote: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.Since you've just discovered one new lookup type, it's not going to shock you that there are other good ones out there, too. Today would be a good day to learn about "__range":" pass in the midnight and 23:59:59 on the day in question and you're down to two lookups. Or use __gte midnight and __lt midnight on the following day. In fact, most databases will treat a date range as being between the two midnights for datetime columns (SQLite is a little fussy there, but MySQL and PostgreSQL behave nicely). So, Q(foo__range=(<day1>, <day1 + one day>)) will likely do what you want. The only reason I'm emphasising these alternatives is that, if you're doing those queries a lot and need to index those columns for speed, setting up an index to allow efficient date range comparisons is easier than creating a functional index for each of the year, month and date lookups (and, if you're using MySQL, say, you can't even do functional indexes). Plus, it's a heck of a lot easier to debug at the Python level with a single datetime object that three variables that have to be train-wrecked together to work out the date each time. 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 -~----------~----~----~----~------~----~------~--~--- |
- 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