【发布时间】:2016-12-01 03:54:07
【问题描述】:
我有以下型号:
class Datacomponent(models.Model):
id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase.
composition = models.ForeignKey(Composition, models.DO_NOTHING, db_column='Composition_ID', null=True, blank=True) # Field name made lowercase.
components = models.ForeignKey(Components, models.DO_NOTHING, db_column='Components_ID') # Field name made lowercase.
componentvalue = models.FloatField(db_column='ComponentValue') # Field name made lowercase.
class Components(models.Model):
id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase.
name = models.CharField(db_column='Name', max_length=45, blank=True, null=True) # Field name made lowercase.
在Datacomponent 中有多行相同的组合(这是Composition 表的外键)。
如果它的每个组件值都在某些值的范围内,我的最终目标是获得一组 Composition。在下面显示的方法中,我根本没有得到任何 composition_ids。
CompositionID ComponentID ComponentValue
1 1 0.5
1 2 0.3
2 1 0.6
2 2 0.4
3 1 0.0
3 2 0.1
所以对上表的查询将是:'获取所有 componentid=1 和 componentvalue__range=(minn[a], maxx[b]) 的组合 ID。如果我们想要componentID 1 的comp 值gte 0.5 和componentID 2 的gte 0.3,我们的结果应该只是2,因为2 的组件1 是0.6,而component2 的值是0.4。
这是我的方法不起作用:
queries = [Q(componentvalue__range = (minn[v], maxx[v]),
components__name = "'"+v+"'",
) for v in minn]
query = queries.pop()
for item in queries:
query |= item
composition_ids = Datacomponent.objects.filter(query).values_list('composition_id', flat=True)
print (composition_ids.count())
【问题讨论】:
标签: python django django-models