Python Django按名称而不是按内容类型id查询内容类型

Python Django按名称而不是按内容类型id查询内容类型,python,sql,django,Python,Sql,Django,我需要计算从我的Flags模型到Post模型的一些关系。现在我不知道如何在我的查询中查询Post模型名称,我知道如何查询content\u type\u id,但不知道如何查询content\u类型(作为字符串)。我脑子里有这样的想法: Q(models.Q(('app_label', 'App'), ('model', 'Post')) 但我不知道在这一点上语法应该是什么样子,可以说smb。帮忙 例如: def post_list_most_bad_flags(request):

我需要计算从我的Flags模型到Post模型的一些关系。现在我不知道如何在我的查询中查询Post模型名称,我知道如何查询content\u type\u id,但不知道如何查询content\u类型(作为字符串)。我脑子里有这样的想法:

Q(models.Q(('app_label', 'App'), ('model', 'Post')) 
但我不知道在这一点上语法应该是什么样子,可以说smb。帮忙

例如:

def post_list_most_bad_flags(request):
    p_counter_query = Flags.objects.filter(content_type_id=12) # --> Want to replace this with content_type instead if content_type_id
    counter = p_counter_query.count()
    print(counter)

提前感谢给定
标志
具有
外键
(或
OneToOneField
)到
ContentType
模型,您可以使用以下选项进行筛选:

def post_list_most_bad_flags(request):
    p_counter_query = Flags.objects.filter(
        content_type__app_label='App',
        content_type__model='Post'
    )
    counter = p_counter_query.count()
    print(counter)
def post_list_most_bad_标志(请求):
p_counter_query=Flags.objects.filter(
内容\类型\应用\标签='app',
内容\类型\模型='Post'
)
counter=p\u counter\u query.count()
打印(计数器)