如何通过Django创建营业时间

时间:2018-04-04 01:45:27

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

我正在创建一个程序,允许用户注册他们的餐馆以进行交付计划,但是,我正在尝试实施方法,允许用户在注册时输入他们的营业时间。到目前为止我得到了这个:

Models.py



WEEKDAYS = [
    (1, _("Monday")),
    (2, _("Tuesday")),
    (3, _("Wednesday")),
    (4, _("Thursday")),
    (5, _("Friday")),
    (6, _("Saturday")),
    (7, _("Sunday")),
    ]

    weekday = models.IntegerField(
        choices=WEEKDAYS,
        unique=True
    )
    from_hour = models.TimeField()
    to_hour = models.TimeField()




Forms.py



class RestaurantForm(forms.ModelForm):

    class Meta:
        model = Restaurant

        fields = ("restaurant_name", "phone", "logo", "tagline", "street_address",
         "state", "city", "zip_Code","weekday", "from_hour", "to_hour")




但是,当我运行它时,选项会以不可用的格式显示,不允许用户输入超过日期和时间:

enter image description here

我不知道应该怎么做才能设置此表单并允许餐馆添加他们打开或关闭的所有日期和时间。

1 个答案:

答案 0 :(得分:1)

您需要创建新模型OpenningTime

使用week_dayfrom_hourto_hour

然后在ManyToManyField的餐厅中设置OpenningTime

现在您可以在餐厅添加多天

  WEEKDAYS = [
    (1, _("Monday")),
    (2, _("Tuesday")),
    (3, _("Wednesday")),
    (4, _("Thursday")),
    (5, _("Friday")),
    (6, _("Saturday")),
    (7, _("Sunday")),
 ]
class OpenningTime(models.Model):

    weekday = models.IntegerField(
        choices=WEEKDAYS,
        unique=True)
    from_hour = models.TimeField()
    to_hour = models.TimeField()

class Restaurant(models.Model):
    openning_times = models.ManyToManyField(OpenningTime)