How to make a model not available on sertain dates and hours in Django?

So, I’m creating a room renting project. The renting is not for a whole day, it’s only for 2 hours. I’m gonna have several renting companies each having several rooms for rent with diffrent capacity. So, my question is, if a room is rented on lets say 25/03/2022 20:00(so its gonna be unvailable until 22:00, and also unavailable 2 hours prior), I want the room to be still visible on the site, because someone maybe will want to rent it on diffrent day or the same day in diffrent hour. How to make this exact room unavailable only on this date and this hours? If I make is_available row in the model its gonna switch to False whenever someone rent it, but I want it to be available for all the other days and hours. Here are the models:

class Owner(models.Model):
    name = models.CharField(max_length=100)
    address = models.TextField()
    phone = models.PositiveIntegerField()

class Room(models.Model):
    number = models.CharField(max_length=100)
    owner = models.ForeignKey(Owner, on_delete=models.CASCADE)
    capacity = models.PositiveIntegerField()
    is_available = models.BooleanField(default=True)

class reservation(models.Model):
    room_number = models.ForeignKey(Room)
    guest_name = models.CharField(max_length=200)
    guests = models.PositiveIntegerField()
    date_reserved = models.DateTimeField()

Hi olof44,

As you correctly pointed out, you can’t use is_available in the regards to availability to rent. is_available would have to be something that would be used to prevent any type of reservation from being made, such as if its having the bathroom updated, flooring redone, etc.

To determine if an rentable room is available, you’ll need to look that up from your reservation model and determine if there are any windows open for renting. You would want to fetch reservations for a given date range (maybe a week or month depending on the frontend), then render those occupancies plus any overhead time (you said 2 hours prior) on the frontend. When confirming the reservation, you’ll want to do the check one more time before saving to confirm there aren’t conflicts.

1 Like