Reverse date/time filtering the model

I’m building a room rent site, so I have these models:

class Hotels(models.Model):
name = models.CharField(max_length=30, unique=True)
slug = models.SlugField(max_length=30, unique=True)
user = models.ForeignKey(Account, on_delete=models.CASCADE)
address = models.CharField(max_length=150)
website = models.CharField(max_length=100, blank=True, null=True)
email = models.EmailField()
phone = models.CharField(max_length=13)
available = models.BooleanField(default=False)
description = models.TextField(blank=True, max_length=1000)
logo = models.ImageField(upload_to=‘Restaurant_Logos’)
date_added = models.DateTimeField(auto_now_add=True)

class Rooms(models.Model):
number = models.CharField(max_length=20)
slug = models.SlugField(max_length=20)
hotel = models.ForeignKey(Hotels, on_delete=models.CASCADE)
seats = models.PositiveIntegerField()
available = models.BooleanField(default=True)

class Reservations(models.Model):
number = models.PositiveIntegerField()
hotel = models.ForeignKey(Hotels, on_delete=models.CASCADE)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
room = models.ForeignKey(Room, on_delete=models.CASCADE)
people = models.PositiveIntegerField()
price = models.DecimalField()
date_time = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)

I will have more than one hotel on the site, every of them will have their rooms.
When somebody reserve a room, the rooms will be reserved only for 2 hours not for a whole day.
On the page I’m building there is a filter menu with date and time pickers,
So my question is, How to filter all the rooms throght the Reservation model, so I can display the hotels that have a free room on this day and hour?
But if somebody wants to reserve a room, this room shouldn’t be reserved 2 hours prior and 2 hours afterwards the given date/time from the user because all the rooms are reserved for 2 hours.
So I have to filter the hotels with that exception in mind.

If anybody help me I’m gonna be really gratefull.
Thank you!

First, look at this in the opposite direction - you should be able to query for all rooms having a reservation +/- two hours from a given datetime.

The rooms you’re looking for then are all the other rooms.

Yes, that was what I tried to say.
But the problem is how to exclude these rooms after I filter them, and I’m not sure how exactly to filter with datetime +/-

See exclude

Forget datetime for a moment. If you had an integer “x”, how would you filter an integer field such that it’s between “x - 2” and “x + 2”?

1 Like

If it’s an integer, it should be something like this:

find = Numbers.objects.filter(number__gt=number-2).filter(number__lt=number+2)

I guess

Good, that works. Now, how do you add or subtract 2 hours to a datetime field? If you’re unsure of the answer, check out the Python timedelta object.

1 Like

Thank you very much, I’ve done it :slight_smile:
I love the way you help people and in the same time make them learn how to do it themselves!
Thank you for your support!