vendredi 14 novembre 2014

python - Django une boucle à travers horaires - Stack Overflow


This is my code for displaying Restaurant Business Hours:


<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>

When I run this, the output is:


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.

My goal is to have it grouped by day of the week so the output is something like this:


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.
...

Any suggestions?


EDIT: (models added)


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'



This looks like a good candidate for the regroup tag. 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>

EDIT: With the way the related models are configrured. It'd probably be easier to create a dictionary in the view code by looping through your queryset and providing the template with something similar to this:


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

Without being able to test it, this is how I imagine forming the output would probably look:


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}]


This is my code for displaying Restaurant Business Hours:


<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>

When I run this, the output is:


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.

My goal is to have it grouped by day of the week so the output is something like this:


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.
...

Any suggestions?


EDIT: (models added)


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'


This looks like a good candidate for the regroup tag. 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>

EDIT: With the way the related models are configrured. It'd probably be easier to create a dictionary in the view code by looping through your queryset and providing the template with something similar to this:


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

Without being able to test it, this is how I imagine forming the output would probably look:


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}]

0 commentaires:

Enregistrer un commentaire