I'm not sure how to do directly in Django ORM, but if you use plain SQL queries, you could do try:
select * from conference_room cr1 where cr1.start_time >= (select max(start_time) from conference_room cr2 where cr2.start_time < :yout_filter_time) and cr1.start_time < :your_filter_time the "select max" gives you the row immediately before your time range. if you don't want to use a plain SQL approach, you will have to do two accesses to the DB. then, there are several way to do so, like: ConferenceRoom.objects.filter(start_time__gt=ConferenceRoom.objects.filter(start_time__lt=your_start_time).aggregate(Max('start_time')) [:1].get().start_time).filter(start_time__lt=your_end_time) sorry. I'm not sure if I typed correctly, but it's the same idea of the SQL statement I mentioned before: 1. find the maximum date before your the start date of your time frame 2. find all events after that date (1) including it (greater and equal to) and before your end date hope it helps, regards, --------- On 10 jun, 10:56, illuminated <petrovic.mi...@gmail.com> wrote: > Hi all, > > I'm writing a django app and need help with filtering results from DB. > > There is a conference room and it's usage is stored in django model. > There is a field "start_time" (model.TimeField); no "end_time". When I > query the table with a time range (i.e. 3pm - 5pm) I would like to get > not only events that start within given time range, but also the one > currently running (the last one that started before 3pm). > > The only solution I have in my mind at the moment is to have two > queries: one would return events starting within the range, other > would retrieve events before that, would sort them descending by time > and I'd get just the first row. Then I'd try to join these two > results. > > Although the above algorithm would/should work, I was wondering if > there was more elegant way to this? > > Thanks! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.