I have to store restaurants on my table in the Django model. Each restaurant has its opening and closing time. For each I have to check if a particular restaurant is open or not with respect to the current time a user search for available restaurants.
Asked
Active
Viewed 603 times
-3
-
where is your model ? – Nishant Nawarkhede Jun 21 '18 at 12:24
-
and? you need to show us your models right! – Hozayfa El Rifai Jun 21 '18 at 12:27
-
We have no information but considering your question [DateTimeField](https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.DateTimeField) make sense. – scharette Jun 21 '18 at 12:30
1 Answers
2
For the comparison, and a very simple case, you would do something like this:
is_open = user_request_time > restaurant.open and user_request_time < restaruant.close
But this is probably too simple. Restaurants are open different hours on different days.
So you might try giving a Schedule
object to each restaurant:
class Schedule(models.Model):
sunday_open = TimeField()
sunday_close = TimeFiled()
....
Then make a class method in Restaurant:
class Restaurant(models.Model):
schedule = models.ForeignKey(Schedule)
....
def is_open(self, time=None)
if not time:
time = timezone.now()
today = time.weekday()
if today == 0:
open = self.schedule.sunday_open
close = self.schedule.sunday_close
elif today == 1:
....
return time > open and time < close
This is where I think I would start with something like this.

Rob L
- 3,634
- 2
- 19
- 38