【问题标题】:Show if restaurant is open or closed based on weekday, opening and closing time根据工作日、营业时间和关闭时间显示餐厅是否营业或关闭
【发布时间】:2016-08-21 07:09:34
【问题描述】:

我有一个具有外键关系的餐厅和营业时间模型。 例如一个餐厅海景咖啡厅,每个工作日的营业时间是

星期五 - 上午 10 点 - 下午 6:30

星期四 - 上午 10 点 - 晚上 9:45

星期三 - 上午 10 点 - 晚上 9:45

星期二 - 上午 10 点 - 晚上 9:45

星期一 - 上午 10 点 - 晚上 9:45

周日 - 上午 10 点 - 晚上 8 点

我只能像这样根据开闭时间来检查

models.py

class OperatingTime(models.Model):
    MONDAY = 1
    TUESDAY = 2
    WEDNESDAY = 3
    THURSDAY = 4
    FRIDAY = 5
    SATURDAY = 6
    SUNDAY = 7

    DAY_IN_A_WEEK = (
        (MONDAY, 'Monday'),
        (TUESDAY, 'Tuesday'),
        (WEDNESDAY, 'Wednesday'),
        (THURSDAY, 'Thursday'),
        (FRIDAY, 'Friday'),
        (SATURDAY, 'Saturday'),
        (SUNDAY, 'Sunday'),
        )
    # HOURS = [(i, i) for i in range(1, 25)]
    restaurant = models.ForeignKey(Restaurant,related_name="operating_time")
    opening_time = models.TimeField()
    closing_time = models.TimeField()
    day_of_week = models.IntegerField(choices=DAY_IN_A_WEEK)

    @property
    def open_or_closed(self):
        operating_time = OperatingTime.objects.all()
        opening_time = operating_time.opening_time
        closing_time = operating_time.closing_time
        current_time =  datetime.now().time()
        weekday = operating_time.day_of_week
        if opening_time < current_time < closing_time:
            open_or_closed = True
        else:
            open_or_closed = False 
        return open_or_closed


@property
def open_or_closed(self):
    operating_time = OperatingTime.objects.all()
    opening_time = operating_time.opening_time
    closing_time = operating_time.closing_time
    current_time =  datetime.now().time()
    weekday = operating_time.day_of_week
    if opening_time < current_time < closing_time:
        open_or_closed = True
    else:
        open_or_closed = False 
    return self.open_or_closed

我如何知道所有餐厅的餐厅每天营业还是关闭?

【问题讨论】:

    标签: python django python-3.x django-models


    【解决方案1】:

    未经测试,但应该可以工作。

    class OperatingTimeManager(models.Manager):
        # Use a model manager when accesing table data, use model methods only for row operations
    
        def get_open_restaurants(self, date=datetime.datetime.now()):
            # get the id's of open restaurants
            open_restaurants_ids = self.get_queryset().filter(opening_time__lte=date, closing_time__gt=date).values_list('restaurant__id', flat=True)
            # get the actual restaurants instances
            return Restaurant.objects.filter(id__in=open_restaurants_ids)
    
    
    class OperatingTime(models.Model):
        closing_time = fields.DateTimeField()
        opening_time = fields.DateTimeField()
        restaurant = fields.ForeignKey(Restaurant)
    
        objects = OperatingTimeManager()
    

    【讨论】:

    • 如何查看每天的开盘和收盘时间?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2012-01-21
    • 2010-10-20
    相关资源
    最近更新 更多