Django queryset过滤模型属性与其他模型属性

时间:2022-10-04 15:13:43

I don't know if I made myself clear with this question title, but, heres my problem:

我不知道我是否清楚这个问题标题,但是,这是我的问题:

I have this model, which is just a transactional model:

我有这个模型,它只是一个事务模型:

class InstanceItemEvaluation(models.Model):
    instance = models.ForeignKey(Instance)
    item = models.ForeignKey(InstanceItem)
    user = models.ForeignKey(User)
    factor = models.ForeignKey(Factor)
    measure = models.ForeignKey(Measure)
    measure_value = models.ForeignKey(MeasureValue, null=True, blank=True)
    evaluated_at = models.DateTimeField(null=True, blank=True)

Here is a query I must run to only retrieve valid values from the database:

这是我必须运行的查询,只从数据库中检索有效值:

@staticmethod
def get_user_evaluations_by_instance(user, instance):
    qs = InstanceItemEvaluation.objects.filter(
        user=user, 
        instance=instance, 
        factor__is_active=True, 
        measure__is_active=True).exclude(
        factor__measure=None)
    return qs

The query set speaks for itself, I am just filtering the user, and the working instance and so on. This query set output this SQL:

查询集说明了一切,我只是过滤用户,工作实例等等。此查询集输出此SQL:

SELECT "workspace_instanceitemevaluation"."id", 
       "workspace_instanceitemevaluation"."instance_id", 
       "workspace_instanceitemevaluation"."item_id", 
       "workspace_instanceitemevaluation"."user_id", 
       "workspace_instanceitemevaluation"."factor_id", 
       "workspace_instanceitemevaluation"."measure_id", 
       "workspace_instanceitemevaluation"."measure_value_id", 
       "workspace_instanceitemevaluation"."evaluated_at" 
  FROM "workspace_instanceitemevaluation" 
 INNER JOIN "measures_measure" ON ( "workspace_instanceitemevaluation"."measure_id" = "measures_measure"."id" ) 
 INNER JOIN "factors_factor" ON ( "workspace_instanceitemevaluation"."factor_id" = "factors_factor"."id" ) 
 WHERE ("measures_measure"."is_active" = True 
   AND "workspace_instanceitemevaluation"."user_id" = 1 
   AND "factors_factor"."is_active" = True 
   AND "workspace_instanceitemevaluation"."instance_id" = 5 
   AND NOT ("factors_factor"."measure_id" IS NULL));

So far so good. But now I need to put this clause on the query:

到现在为止还挺好。但是现在我需要在查询中加上这个子句:

   AND "factors_factor"."measure_id" = "measures_measure"."id"

Which would mean I am only looking for measure values that are currently associated with my factors. Anyway, I tried to do something like this (look at the last filter):

这意味着我只想查找当前与我的因素相关的度量值。无论如何,我试图做这样的事情(看看最后一个过滤器):

@staticmethod
def get_user_evaluations_by_instance(user, instance):
    qs = InstanceItemEvaluation.objects.filter(
        user=user, 
        instance=instance, 
        factor__is_active=True, 
        measure__is_active=True).exclude(
        factor__measure=None).filter(
        factor__measure=measure)
    return qs

But that doesn't even make sense. Now I am kinda stuck, and couldn't find a solution. Of course that's something I can do iterating the result and removing the results I don't need. But I am trying to figure out if it is possible to achieve this SQL query I mentioned using the Django queryset API.

但这甚至没有意义。现在我有点卡住,无法找到解决方案。当然,这是我可以做的事情迭代结果并删除我不需要的结果。但我试图弄清楚是否有可能使用Django queryset API实现我提到的SQL查询。

1 个解决方案

#1


1  

I'm not sure if it will work in this case, but generally you can use F() objects for this.

我不确定它是否适用于这种情况,但通常你可以使用F()对象。

from django.db.models import F
qs = InstanceItemEvaluation.objects.filter(
    user=user, 
    instance=instance, 
    factor__is_active=True, 
    measure__is_active=True).exclude(
    factor__measure=None).filter(
    factor__measure=F('measure_id'))

#1


1  

I'm not sure if it will work in this case, but generally you can use F() objects for this.

我不确定它是否适用于这种情况,但通常你可以使用F()对象。

from django.db.models import F
qs = InstanceItemEvaluation.objects.filter(
    user=user, 
    instance=instance, 
    factor__is_active=True, 
    measure__is_active=True).exclude(
    factor__measure=None).filter(
    factor__measure=F('measure_id'))