Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Objects for business hours

I'm trying to modelize business hours for a week of a company. Here is my attempt:

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)

Does this seem correct ?

Isn't there a more dynamic way to define it ?

Can I easily validate each day (on a form) ?

like image 455
Pierre de LESPINAY Avatar asked Aug 31 '12 13:08

Pierre de LESPINAY


1 Answers

From @lyapun suggestion, an acceptable solution for me would be:

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()
like image 156
Pierre de LESPINAY Avatar answered Sep 23 '22 06:09

Pierre de LESPINAY