James L.
James L.

Reputation: 1153

Business Opening hours in Django

I want to add Business hours for a clinic and I've looked into this Any existing solution to implement "opening hours" in Django but it's not right for me. Because this one assumes you'll have same set of hours for all the weekdays and same set of hours for special days. Whereas, I want to have different opening hours for individual days. Moreover, I want to have more than 1 entry for an individual day. For instance, a clinic on Sunday runs from 8:30 am - 12:00 pm and opens again from 4:30 pm - 10 pm.

I want to be able to add this from the admin panel, similar to Yelp

enter image description here

Upvotes: 9

Views: 4882

Answers (1)

catavaran
catavaran

Reputation: 45575

IMHO the solution from the link is doing almost exactly what you want. Just customize it a bit:

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

class OpeningHours(models.Model):

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

    class Meta:
        ordering = ('weekday', 'from_hour')
        unique_together = ('weekday', 'from_hour', 'to_hour')

    def __unicode__(self):
        return u'%s: %s - %s' % (self.get_weekday_display(),
                                 self.from_hour, self.to_hour)

Upvotes: 16

Related Questions