I have a Django model as shown below -
class Unavailability(BaseModel):
yacht = models.ForeignKey(
YachtGeneralInfo,
related_name="yacht_unavailability",
on_delete=models.PROTECT,
blank=False,
null=False,
)
start_date = models.DateField(blank=False, null=False, unique=True)
end_date = models.DateField(blank=False, null=False, unique=True)
class Meta:
unique_together = ["start_date", "end_date"]
I want to write a method which will take a date as an argument and returns True if the date isn't in between any of the start date and end date of the records in the model Unavailability.
I thought of one approach where I will fetch all the rows for a given yacht and then while iterating over the returned result I will check if the given date is between any of the start date and end date. If the date is in between any of the start date and end date then the method would immediately return False or else it will return True.
The problem with this approach is if there are too many records in the model for the given yacht then it will iterate too many times. Also, the start date and end_date could be across months and years so couldn't even filter the unavailability of a given based on month and year.
How you have approached this problem with the most efficiency?