如果您要使用其他查询集中的值来过滤查询集,那么将“过滤器”值注释到原始查询集上的最佳方法是什么?
例如,下面有一个使用simKeys QS进行过滤的simGrants QS。找到匹配项后,我想将匹配的simKeys余弦分数附加到simGrants对象。
数据库相当大,因此我正在寻找一种计算效率最高的方法来执行此操作。
Simkeys =similarity_matrix.objects.filter(y_axis__in=profiles).filter(cosine_score__gt=0)
simGrants = Grant.objects.filter(indexKey__in=simKeys)
答案 0 :(得分:0)
您可以使用相关模型的字段F
-expression [Django-doc]对DoCmd.OpenReport "rptStageThree", acViewReport, , qry, , "[SUBURB] ASC" ' "[SUBURB] DESC" for sort descending, multiple fields seperated by comma "[Suburb], [Sale Price] DESC, [Land]"
对象进行注释。这将导致查询如下:
Grant
此查询集产生的from django.db.models import F
Grant.objects.annotate(
cosine_score=F('similarity_matrix__cosine_score')
).filter(
similarity_matrix__y_axis__gt=0,
similarity_matrix__cosine_score__gt=0
)
对象将具有一个额外的属性Grant
,它是相关cosine_score
对象的cosine_score
。