我正在try 构建一个仅添加假值的查询集.

模型

模型
reference = models.CharField(validators=[MinLengthValidator(15)], max_length=25, primary_key=True)
h0730 = models.BooleanField(default=False)
h0800 = models.BooleanField(default=False)
h0830 = models.BooleanField(default=False)
h0900 = models.BooleanField(default=False)
h0930 = models.BooleanField(default=False)
h1000 = models.BooleanField(default=False)
h1030 = models.BooleanField(default=False)
h1100 = models.BooleanField(default=False)
h1130 = models.BooleanField(default=False)
h1200 = models.BooleanField(default=False)
h1230 = models.BooleanField(default=False)
h1300 = models.BooleanField(default=False)
h1330 = models.BooleanField(default=False)
h1400 = models.BooleanField(default=False)
h1430 = models.BooleanField(default=False)
h1500 = models.BooleanField(default=False)
h1530 = models.BooleanField(default=False)
delivery_date = models.CharField(max_length=8)
is_cancelled = models.BooleanField(default=False)

视图

taken_slots = Order.objects.filter(delivery_date__exact=delivery_date).filter(reference__icontains=code).filter(is_cancelled=False)

slots_remaining = ['h0730', 'h0800', 'h0830', 'h0900', 'h0930', 'h1000', 'h1030', 'h1100', 'h1130', 'h1200', 'h1230', 'h1300', 'h1330', 'h1400', 'h1430', 'h1500', 'h1530']

for slot in taken_slots:
    if slot.h0730 and 'h0730' in slots_remaining:
        slots_remaining.remove('h0730')
    if slot.h0800 and 'h0800' in slots_remaining:
        slots_remaining.remove('h0800')
        ...
        ...

上面的for循环按照预期工作,但我正在try 优化该过程.例如,如果这一天有100个引用,则"takeslot"将被迭代100次.

在for循环完成后的预期输出是,"SLOGES_RELEVING"列表将只剩下FALSE值,例如

ref1 = h0730 and h0930 is True and every other slot False
ref2 = h0900 is True and every other slot False
ref3 = h1030 is True and every other slot False
ref4 = h1230 is True and every other slot False
ref5 = h1300 and h1330 is True and every other slot False
ref6 = h1500 is True and every other slot False

If h0730, h0900, h0930, h1030, h1230, h1300, h1330, h1500 from the 6 references are True.
slots_remaining must remain with ['h0800', 'h0830', 'h1000', 'h1100', 'h1130', 'h1200', 'h1400', 'h1430', 'h1530']

有没有办法直接从TAKE_SLOTS查询构建SLOTS_RELEVING列表,或者至少避免FOR循环.我见过DISTINCT、ANNOTATE和UNION的例子,但可能做错了,因为我没能让它工作.

编辑: 为了添加更多上下文,一个引用的查询集示例如下所示:

<QuerySet [{'h0730': True, 'h0800': False, 'h0830': False, 'h0900': False, 'h0930': False, 'h1000': False, 'h1030': False, 'h1100': False, 'h1130': False, 'h1200': False, 'h1230': False, 'h1300': False, 'h1330': False, 'h1400': True, 'h1430': True, 'h1500': True, 'h1530': True}]>

我试图完成的基本上是多个引用的所有值的合并.例如,下面的6个引用将是单个查询集的一部分

ref_start = ['h0730', 'h0800', 'h0830', 'h0900', 'h0930', 'h1000', 'h1030', 'h1100', 'h1130', 'h1200', 'h1230', 'h1300', 'h1330', 'h1400', 'h1430', 'h1500', 'h1530']

ref1 = ['True', 'h0800', 'h0830', 'True', 'h0930', 'h1000', 'h1030', 'h1100', 'h1130', 'h1200', 'h1230', 'h1300', 'h1330', 'h1400', 'h1430', 'h1500', 'h1530']
ref2 = ['h0730', 'h0800', 'h0830', 'h0900', 'True', 'h1000', 'h1030', 'h1100', 'h1130', 'h1200', 'h1230', 'h1300', 'h1330', 'h1400', 'h1430', 'h1500', 'h1530']
ref3 = ['h0730', 'h0800', 'h0830', 'h0900', 'h0930', 'h1000', 'True', 'h1100', 'h1130', 'h1200', 'h1230', 'h1300', 'h1330', 'h1400', 'h1430', 'h1500', 'h1530']
ref4 = ['h0730', 'h0800', 'h0830', 'h0900', 'h0930', 'h1000', 'h1030', 'h1100', 'h1130', 'h1200', 'True', 'h1300', 'h1330', 'h1400', 'h1430', 'h1500', 'h1530']
ref5 = ['h0730', 'h0800', 'h0830', 'h0900', 'h0930', 'h1000', 'h1030', 'h1100', 'h1130', 'h1200', 'h1230', 'True', 'True', 'h1400', 'h1430', 'h1500', 'h1530']
ref6 = ['h0730', 'h0800', 'h0830', 'h0900', 'h0930', 'h1000', 'h1030', 'h1100', 'h1130', 'h1200', 'h1230', 'h1300', 'h1330', 'h1400', 'h1430', 'True', 'h1530']

ref_merged = ['True', 'h0800', 'h0830', 'True', 'True', 'h1000', 'True', 'h1100', 'h1130', 'h1200', 'True', 'True', 'h1330', 'h1400', 'h1430', 'True', 'h1530']

ref_merged_without_True = ['h0800', 'h0830', 'h1000', 'h1100', 'h1130', 'h1200', 'h1330', 'h1400', 'h1430', 'h1530']
    

是否可以直接从查询中获得ref_merged_without_True,或者循环是必要的?

推荐答案

我不明白您为什么要列举100次,您可以使用一个集合,从而使用:

slots_remaining = {
    'h0730',
    'h0800',
    'h0830',
    'h0900',
    'h0930',
    'h1000',
    'h1030',
    'h1100',
    'h1130',
    'h1200',
    'h1230',
    'h1300',
    'h1330',
    'h1400',
    'h1430',
    'h1500',
    'h1530',
}


for slot in taken_slots:
    slots_remaining -= {name for name in slot_remaining if getattr(slot, name)}

这将在taken_slots中枚举once,并且对于每个插槽,每次只判断剩余的一次.在循环结束时,剩余的一次仍在slots_remaining.因此,它将查询数据库once.

但建模看起来并不是很有效:数据库通常是should be designed lineary个.这意味着两个项目属于相同的"类别",不要将它们存储在不同的列中,而是存储在不同的行中.这使得查询变得更加方便,尤其是在本例中.

Python相关问答推荐

如何在具有重复数据的pandas中对groupby进行总和,同时保留其他列

使用FASTCGI在IIS上运行Django频道

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

如何在Python中并行化以下搜索?

我们可以为Flask模型中的id字段主键设置默认uuid吗

如何使用pytest来查看Python中是否存在class attribution属性?

如何从数据库上传数据到html?

将输入聚合到统一词典中

Plotly Dash Creating Interactive Graph下拉列表

Python Tkinter为特定样式调整所有ttkbootstrap或ttk Button填充的大小,适用于所有主题

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送

如何获取Python synsets列表的第一个内容?

Gekko中基于时间的间隔约束

如果包含特定值,则筛选Groupby

如果有2个或3个,则从pandas列中删除空格

如何获得3D点的平移和旋转,给定的点已经旋转?

Matplotlib中的曲线箭头样式

解析CSV文件以将详细信息添加到XML文件

为什么在安装了64位Python的64位Windows 10上以32位运行?

Python:使用asyncio.StreamReader.readline()读取长行