我有两个这样的模型:
class Schedule(models.Model):
name = models.CharField(_('name'), blank=True, max_length=15)
class Day(models.Model):
DAYS_OF_THE_WEEK = (
(0, _('Monday')),
(1, _('Tuesday')),
(2, _('Wednesday')),
(3, _('Thursday')),
(4, _('Friday')),
(5, _('Saturday')),
(6, _('Sunday')),
)
schedule = models.ForeignKey(Schedule, blank=True, null=True, verbose_name=_('schedule'))
day = models.SmallIntegerField(_('day'), choices=DAYS_OF_THE_WEEK)
opening = models.TimeField(_('opening'), blank=True)
closing = models.TimeField(_('closing'), blank=True)
时间表可能有两个 Day 对象,如下所示:
天(schedule=1, day=0, opening=datetime.time(7, 30), close=datetime.time(10, 30)) Day(schedule=1, day=0, opening=datetime.time(12, 30), 关闭=datetime.time(15, 30))
就像同一天的不同班次一样。
如果我现在迭代它们,我将得到第 0 天的两个条目,就像这样 [日程中的每一天] [0, 0, 1, 2, 3, 4, 5, 6]
如何创建一个查询集,以便将同一天组合在一起并保留它们的属性?
[day for day in schedule]
[0 (two entries), 1, 3, 4, 5, 6]
也许像
[id: [day], id: [day]]