【问题标题】:How to check and store particular time-period in Django? [closed]如何在 Django 中检查和存储特定时间段? [关闭]
【发布时间】:2018-11-30 18:46:55
【问题描述】:

我必须在 Django 模型中将餐厅存储在我的桌子上。每家餐厅都有开放和关闭时间。对于每一个,我必须根据用户搜索可用餐厅的当前时间检查特定餐厅是否营业。

【问题讨论】:

  • 你的模特在哪里?
  • 和?您需要向我们展示您的模型!
  • 我们没有任何信息,但考虑到您的问题DateTimeField 是有道理的。

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


【解决方案1】:

为了比较,一个非常简单的例子,你可以这样做:

 is_open = user_request_time > restaurant.open and user_request_time < restaruant.close

但这可能太简单了。餐厅在不同日子的营业时间不同。

所以你可以尝试给每家餐厅一个Schedule 对象:

class Schedule(models.Model):
    sunday_open = TimeField()
    sunday_close = TimeFiled()
    ....

然后在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

这是我想我会从这样开始的地方。

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2016-06-17
    相关资源
    最近更新 更多