You have a problem with your calculation, if you dont want any overlap at all. The above code will only be true when the intire range of start/end 2 is within start/end 1. what you need to check if you dont want any overlap is these two statements: (start1 <= start2 and end1 >= start2) (start1 <= endt2 and end1 >= end2)
Basically what you need to check for is if either start or end date is inside the start/end date range. If one of the dates is inside the range, you will have partial overlap. On 9 Mar., 16:28, gnijholt <gijs.nijh...@gmail.com> wrote: > Hello django-users, > > I'm having some trouble with a date-range filter. > My goal is to prevent a model from being saved when it's date-range > overlaps with existing records. > > Apparently, the math is quite straightforward: > > ( start1 <= end2 and start2 <= end1 ) > > if TRUE, the ranges overlap (*) > > Still, I can't get it to work. Am I doing it wrong? > I've tried some combinations, such as: > > SomeModel.objects.filter( Q(time_start__lte=self.time_end) & Q > (time_end__gte=self.time_end) ).count() > > Thanks for any help or hints. > > Gijs > > (*http://c2.com/cgi/wiki?TestIfDateRangesOverlap) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---