[英]Django query content_type by name not content_type_id
我需要計算從我的標志 model 到我的帖子 Model 的一些關系。現在我不明白如何在我的查詢中查詢帖子 model 名稱,我了解如何查詢 content_type_id 但不是查詢 content_type(作為字符串). 我腦子里有這樣的事情:
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)
提前致謝
鑒於Flags
具有ForeignKey
(或OneToOneField
到ContentType
model,您可以使用以下方式進行過濾:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.