假设我有一个来自Swallow -=> Coconut的ManyToManyField。
另外,假设Coconut有一个名为“Young”的布尔值字段。
对于一只给定的燕子,我怎么知道它所有的椰子都是幼小的?
(我真正寻找的不是判断燕子的椰子是否年轻的能力,而是判断查询集中的项目之间是否存在差异值的方法。)
发布于 2011-03-25 05:34:42
好了,我想通了。我需要:
swallow.coconuts.aggregate(test=Variance('young'))如果是0,我知道它们都是一样的。
发布于 2011-03-25 05:13:59
if there is no `coconut` for a `swallow` that is `not young`
then all `coconuts` are `young`编辑:
据我所知,没有queryset方法可以做到这一点,但您可以创建自己的方法,例如:
# 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})https://stackoverflow.com/questions/5425582
复制相似问题