12

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

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)

这看起来正确吗?

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

我可以轻松地验证每一天(在表格上)吗?

4

1 回答 1

19

根据@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()
于 2012-08-31T14:14:14.217 回答