Django queryset:如何根据查询集字段过滤RawSQL注释

时间:2017-11-03 12:17:30

标签: django

是否可以在Django中执行类似的操作:

MyModel.objects.annotate(
    val=RawSQL(
        "SELECT COUNT(*) FROM another_model_table where some_field= %s",
            (a_field_from_MyModel)
        )
    )

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以这样做:

MyModel.objects.annotate(
    val=RawSQL(
        """SELECT COUNT(*) FROM another_model_table 
            where some_field=myapp_mymodel.some_field""",
    )
)