如何为以下情况定义查询过滤器?

How to define the query filter for the following situation?

我已经定义了我的模型如下,以及如何定义一个查询来过滤 Sanity_Test 集合,其中关联的 Test_Product 的产品名称在指定的名称列表中?非常感谢

例如:

  1. Sanity_Test_A 包含 Test_Product_A(产品名称为 A),Test_Product_B(产品名称为 B)

  2. Sanity_Test_B 包含 Test_Product_C(产品名称为 A),Test_Product_D(产品名称为 C)

  3. 我想过滤 Sanity_Test 记录列表,它是 Test_Product 记录的产品名称是 A

应用程序:CT.model

class Sanity_Test(models.Model):

    build        = models.OneToOneField('CI.Build')                                                           
    system_test  = models.ForeignKey(System_Test,null=True,blank=True)

    ......


class Test_Product(models.Model):

    product     = models.ForeignKey('CI.Product',verbose_name='Product')            
    sanity_test = models.ForeignKey(Sanity_Test)

    ......


# APP : CI.model 
class Product(models.Model):

    name = models.CharField(max_length=255,unique=True)

    ......

The Test_Product Record Info

您可以这样做:

sanity_test = [ test_product.sanity_set for test_productin Test_Product.objects.filter(product__name='A')]

这会得到产品名称为A的所有Test_Product对象,然后将对应的sanity_test对象添加到列表

您也可以使用 reverse relationships:

仅通过一个查询来完成
sanity_test = Sanity_Test.objects.filter(test_product__product__name='A')