以 Django 的多对多关系为例:https://docs.djangoproject.com/en/3.1/topics/db/examples/many_to_many/
我希望能够在 Django Admin 中显示文章列表视图上的一列,其中包含相关出版物的标题。因此,如果我有一篇文章 a1
有出版物:
Publication(title='Pub 1')
Publication(title='Pub 2')
我想在管理列表视图中看到一列显示 "Pub 1, Pub 2"
。我可以使用 Admin 类上的自定义函数来做到这一点:
class ArticleAdmin(admin.ModelAdmin):
list_display = ['publication_titles']
def publication_titles(self, obj):
return ', '.join([pub.title for pub in obj.publications.all()])
但这是一个 N+1 问题:每个文章行都会为每个关联的出版物执行查询。我在 Django 调试工具栏中看到了这一点,在列表视图中渲染 24 篇文章时,我看到执行的 SQL 查询数量从 9 个查询增加到 33 个。
我想也许我可以在查询集中做 prefetch_related
:
class ArticleAdmin(admin.ModelAdmin):
list_display = ['publication_titles']
def get_queryset(self, request):
queryset = super().get_queryset(request)
queryset.prefetch_related('publications')
return queryset
def publication_titles(self, obj):
return ', '.join([pub.title for pub in obj.publications.all()])
但这似乎对执行的 SQL 查询数量没有影响。
我想我可以使用 annotate()
来注释管理员中使用的查询集,但我正在努力弄清楚如何做到这一点。就像我想做的事情一样:
class ArticleAdmin(admin.ModelAdmin):
list_display = ['publication_titles']
def get_queryset(self, request):
queryset = super().get_queryset(request)
queryset.annotate(publication_titles=<HOW DO I GENERATE THE COLLECTION OF TITLES>)
return queryset
但我并不在意我为 <HOW DO I GENERATE THE COLLECTION OF TITLES>
设置的内容。
如何在不增加对数据库的查询数量的情况下显示出版物列表?
答案 0 :(得分:1)
QuerySet
是不可变的,这意味着 .prefetch_related
创建了一个 new QuerySet
,因此您可以使用:
class ArticleAdmin(admin.ModelAdmin):
list_display = ['publication_titles']
def get_queryset(self, request):
return super().get_queryset(request).prefetch_related('publications')
def publication_titles(self, obj):
return ', '.join([pub.title for pub in obj.publications.all()])