How to Constraint Date Range From Intersection

21 Views Asked by At

I have a model defined like below

class Event(models.Model):
    room = models.ForeignKey(Room, on_delete=models.CASCADE)
    start = models.DateField()
    end = models.DateField()

I would like to setup constraints such that time span of each room's event is not intersect with each other.

For example:

  1. Event 1: Room A, date: [2024-01-01, 2024-01-05];
  2. Event 2: Room A, date: [2024-01-08, 2024-01-12]; <- Fine
  3. Event 3: Room A, date: [2024-01-04, 2024-01-07]. <- NOT fine

Event 3 is not fine because Room A is already occupied by Event 1 on Jan 4th and Jan 5th.

How can I achieve this with model constraints?

0

There are 0 best solutions below