假设我有一个ManyToManyField,来自Swallow - =>椰子。
另外,假设椰子有一个名为“Young”的布尔字段。
对于给定的燕子,我怎么能判断它的所有椰子是否都很年轻?
(我真正想要的不仅仅是判断燕子的椰子是否年轻,而是判断查询集中各项之间是否存在差异值的方法。)
答案 0 :(得分:3)
好的,我明白了。我需要:
swallow.coconuts.aggregate(test=Variance('young'))
如果是0,我知道它们都是一样的。
答案 1 :(得分:0)
if there is no `coconut` for a `swallow` that is `not young`
then all `coconuts` are `young`
编辑:
据我所知,没有查询集方法可以做到这一点,但你可以自己创建,如:
# property_values is a dictionary : {prop1:val1, prop2:val2,...}
# you can check for multiple properties
def is_same_property_value(related_manager, property_values)
return related_manager.filter(~Q(**property_values)).count() == 0
res = is_same_property_value(swallow.coconuts_set, {'young':true})