简体   繁体   English

具有相同字段值的组对象

[英]group objects with same field value

I've got two models like these: 我有两个这样的模型:

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)

It's possible that a schedule can have two Day objects like so: 计划可能有两个Day对象,如下所示:

Day(schedule=1, day=0, opening=datetime.time(7, 30), closing=datetime.time(10, 30)) Day(schedule=1, day=0, opening=datetime.time(12, 30), closing=datetime.time(15, 30)) Day(schedule = 1,day = 0,opening = datetime.time(7,30),closing = datetime.time(10,30))Day(schedule = 1,day = 0,opening = datetime.time(12, 30),closing = datetime.time(15,30))

like different shifts on the same day. 喜欢同一天不同的班次。

If I iterate them now i'll get two entries of day 0, like so [day for day in schedule] [0, 0, 1, 2, 3, 4, 5, 6] 如果我现在迭代它们,我会得到第0天的两个条目,就像这样[日程安排] [0,0,1,2,3,4,5,6]

How can I create a queryset so it'll group same days together and keep their attributes? 如何创建一个查询集,以便它将相同的日期组合在一起并保留其属性?

[day for day in schedule]
[0 (two entries), 1, 3, 4, 5, 6]

Maybe something like 也许是这样的

[id: [day], id: [day]]

The code I ended up using is this: 我最终使用的代码是这样的:

from itertools import groupby

day_set = store.schedule_set.all()[0].day_set.all()
    schedule = dict()
    for k, v in groupby(day_set, lambda x: x.day):
        schedule[k] = list(v)

and sending schedule to the template for rendering, which works like a charm. 并将计划发送到模板进行渲染,其效果就像一个魅力。

You can group them at template level, using {% regroup %} or {% for %} -loop with {% ifchanged %} tag. 您可以使用{%regroup%}{% for %} -loop和{%ifchanged%}标记在模板级别对它们进行分组。

In Python code use groupby . 在Python代码中使用groupby

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 group mysql按字段查询具有相同的值和计数 - group mysql query by field with same value and count 如何将数组中对应于相同 JSON 名称/值的所有对象分组 - How to group all objects in an array that correspond to the same JSON name/value 如何在 Pyspark 中按字段对每个组的相同值求和 - How to sum the same value per group by field in Pyspark Django GROUP BY 字段值 - Django GROUP BY field value 如何将具有相同字段值的所有对象显示为 1 结果并在 Django 中聚合另一个字段 - How to show all objects with same field value as 1 result and aggregate another field in django 如何根据具有相同字段值的对象总数在 model 中添加自动增量字段? - How can I add an auto increment field in a model based on total count of objects with same field value? 如何对两个相同的值进行分组并从 django、DRF 中的其他字段获取值 - How to group two same values and get value from other field in django, DRF Django-只有两个对象的外键不同时,它们的字段才能具有相同的值 - Django - Two objects' field can have the same value only if their foreign keys are different 如果python中的多个属性相同,则对对象进行分组 - Group objects if multiple attributes are same in python Pygame,同一组中两个对象之间的碰撞 - Pygame, Collision between 2 objects in the same group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM