I have an issue with finding an overlapping date range in Django. I have two models, reunion
, which has a range of dates:
class reunion(models.Model):
resource = models.ForeignKey(resource, on_delete=models.CASCADE)
start = models.DateTimeField()
end = models.DateTimeField()
title = models.CharField(max_length=100)
And the resource
model:
class resource (models.Model):
site = models.ForeignKey(site, on_delete=models.CASCADE)
name = models.CharField(max_length=60)
def isAvaible(self, endDate, initialDate):
try:
self.reunion_set.get(Q(start__lt=endDate) | Q(end__gt=initialDate))
return False
except:
return True
When I need to make a new reunion with a specific range of dates, I need to find a non-overlapping resource so I use this method:
def getAvaibleAccount(initialDate, endDate):
avaibleResources = resource.objects.all()
for avaibleResource in avaibleResources:
if avaibleResource.isAvaible(initialDate,endDate):
return avaibleResource
return None
But my code says that the date range: (12/30/2019 11:00 - 12/30/2019 12:00) overlaps with (12/31/2019 11:30 - 12/31/2019 12:30) as if just comparing time and not the date. I've been searching a lot and I'm not having any luck.
Where is my error?
I'm getting the dates as strings and parsing them with dateutil.parser.parse()
.