我用ifcahnged
作为
{% for day in doctor.scheduled_by.all %}
{% ifchanged day.start_day_time %}
<span> {{ day }} </span>
<span>{{ day.start_day_time }} AM - {{ day.end_day_time }} PM</span>
{% endifchanged %}
{% endfor %}
,结果为
Saturday
5:00:00 AM - 14:30:00 PM
Friday
0:09:00 AM - 0:02:00 PM
我想要像
这样的东西Saturday - Thursday
5:00:00 AM - 14:30:00 PM
Friday - Saturday
0:09:00 AM - 0:02:00 PM
:)(新社区)。
实际上这就是我的模型。py
class DaySchedulePattern(models.Model):
week_day = models.ForeignKey(WeekDays, on_delete=models.CASCADE)
time_slot_duration = models.DurationField()
start_day_time = models.DurationField()
end_day_time = models.DurationField()
doctor = models.ForeignKey(
"account.Doctor", on_delete=models.CASCADE,related_name="scheduled_by"
)
clinic = models.ForeignKey(Clinic, on_delete=models.CASCADE)
active = models.BooleanField(default=True)
你可以使用django内置过滤器:date
假设模板中的day
是代码中的DateTime(在模型中没有显示),它需要首先添加持续时间。
{% for day in doctor.scheduled_by.all %}
{% ifchanged day.start_day_time %}
<span> {{ day }} </span> - <span> {{ day|add:day.end_day_time|date:'l' }}
<span>{{ day.start_day_time }} AM - {{ day.end_day_time }} PM</span>
{% endifchanged %}
{% endfor %}
可能您可以在account.Doctor
中设置属性end_day
,用于结束日期返回日期值。
@property
def end_day(self):
days, hours, minutes = end_day_time.days, end_day_time.hours, end_day_time.minutes
return day.start_day_time + datetime.timedelta(days=<days>, hours=<hours>).day
{% for day in doctor.scheduled_by.all %}
{% ifchanged day.start_day_time %}
<span> {{ day }} - {{day.end_day}}</span>
<span>{{ day.start_day_time }} AM - {{ day.end_day_time }} PM</span>
{% endifchanged %}
{% endfor %}