我正在尝试为公司一周的营业时间建模。这是我的尝试:

class Company(models.Model):
    name = models.CharField(max_length=100)
    logo = models.FileField(upload_to='company_logos')
    mon_start = models.TimeField()
    mon_end = models.TimeField()
    tue_start = models.TimeField()
    tue_end = models.TimeField()
    wed_start = models.TimeField()
    wed_end = models.TimeField()
    thu_start = models.TimeField()
    thu_end = models.TimeField()
    fri_start = models.TimeField()
    fri_end = models.TimeField()
    sat_start = models.TimeField()
    sat_end = models.TimeField()
    sun_start = models.TimeField(blank=True)
    sun_end = models.TimeField(blank=True)

这看起来正确吗?

有没有更动态的方式来定义它?

我可以轻松地每天(在表单上)进行验证吗?

最佳答案

从@lyapun建议,对我来说可以接受的解决方案是:

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


class Company(models.Model):
    name = models.CharField(
        max_length=100
    )
    logo = models.FileField(
        upload_to='company_logos'
    )



class OpeningHours(models.Model):
    store = models.ForeignKey(
        Company
    )
    weekday = models.IntegerField(
        choices=WEEKDAYS,
        unique=True
    )
    from_hour = models.TimeField()
    to_hour = models.TimeField()

关于Django-营业时间对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12216771/

10-10 06:46