Django在营业时间内循环

时间:2014-03-22 01:04:57

标签: python html django

这是我展示餐厅营业时间的代码:

<dl>
  {% for hours in restaurant.businesshours.all %}
   <dt>{{ hours.dayofweek.name }}</dt>
    <dd>{{ hours.hourstype.name }} {{ hours.opentime.name }} - {{ hours.closetime.name }}</dd>
  {% endfor %}
 </dl>

当我运行它时,输出为:

Monday
Dinner 5:30 p.m. - 10 p.m.
Tuesday
Dinner 5:30 p.m. - 10 p.m.
Wednesday
Dinner 5:30 p.m. - 10 p.m.
Thursday
Dinner 5:30 p.m. - 10 p.m.
Friday
Dinner 5:30 p.m. - 10 p.m.
Saturday
Dinner 5:30 p.m. - 10 p.m.
Sunday
Dinner 5:30 p.m. - 10 p.m.
Sunday
Lunch 11:30 a.m. - 3 p.m.
Monday
Lunch 11 a.m. - 2:30 p.m.
Tuesday
Lunch 11 a.m. - 2:30 p.m.
Wednesday
Lunch 11 a.m. - 2:30 p.m.
Thursday
Lunch 11 a.m. - 2:30 p.m.
Friday
Lunch 11 a.m. - 2:30 p.m.
Saturday
Lunch 11 a.m. - 2:30 p.m.

我的目标是按星期几对其进行分组,以便输出如下:

Monday
-Lunch 11 a.m. - 2:30 p.m. 
-Dinner 5:30 p.m. - 10 p.m.
Tuesday
-Lunch 11 a.m. - 2:30 p.m. 
-Dinner 5:30 p.m. - 10 p.m.
Wednesday
-Lunch 11 a.m. - 2:30 p.m. 
-Dinner 5:30 p.m. - 10 p.m.
...

有什么建议吗?

编辑:(已添加型号)

class Restaurant(models.Model):
name = models.CharField(max_length=50)
businesshours = models.ManyToManyField('Hours', null=True, blank=True, related_name="restaurants")
class Meta:
    db_table = 'restaurant'

class Dayofweek(models.Model):
name = models.CharField(max_length=50, db_column='dayofweek')
class Meta:
    db_table = 'dayofweek'

class Hours(models.Model): 
dayofweek = models.ForeignKey('Dayofweek', null=True, blank=True, related_name="hours")
hourstype = models.ForeignKey('Hourstype', null=True, blank=True, related_name="hours")
opentime = models.ForeignKey('Timeslot', null=True, blank=True, related_name="hours_opentime")
closetime = models.ForeignKey('Timeslot', null=True, blank=True, related_name="hours_closetime")
class Meta:
    db_table = 'hours'

class Hourstype(models.Model): #Lunch, Dinner etc.
name = models.CharField(max_length=50, db_column='hourstype')
class Meta:
    db_table = 'hourstype'

class Timeslot(models.Model): #Each slot is every 15min starting at 5am
name = models.TimeField(db_column='timeslot')
class Meta:
    db_table = 'timeslot'

1 个答案:

答案 0 :(得分:0)

这似乎是regroup标记的一个很好的候选者。 docs

{% regroup restaurant.businesshours.all by dayofweek as hours_list %}
<dl>
    {% for hours in hours_list %}
        <dt>{{ hours.grouper.name }}</dt>
        {% for item in hours.list  %}
            <dd>{{ item.hourstype.name }} {{ item.opentime.name }} - {{ item.closetime.name }}</dd>
        {% endfor %}
    {% endfor %}
</dl>

编辑:通过配置相关模型的方式。通过循环查询集并为模板提供类似于此的内容,可能更容易在视图代码中创建字典:

restaurant_hours = {
    "Monday": [
        {"type": "Breakfast", "hours": "9 a.m. - 12 p.m."},
        {"type": "Lunch", "hours": "12 p.m. - 3 p.m."}
    ]
    ...
}

无法测试它,这就是我想象的形成输出可能看起来:

restaurant_hours = dict()

for hours in restaurant.businesshours.all():
    day = hours.dayofweek.name
    if restaurant_hours.get(day):
        restaurant_hours[day].append({"type": hours.hourstype.name, "hours": hours.opentime.name + " - " + hours.closetime.name})
    else:
        restaurant_hours[day] = [{"type": hours.hourstype.name, "hours": hours.opentime.name + " - " + hours.closetime.name}]