我有一个Person模型,它与Book有外键关系,Book有许多字段,但我最关心的是author(一个标准CharField)。
话虽如此,在我的PersonAdmin模型中,我想显示book。作者使用list_display:
class PersonAdmin(admin.ModelAdmin):
list_display = ['book.author',]
我已经尝试了所有显而易见的方法,但似乎都不起作用。
有什么建议吗?
我有一个Person模型,它与Book有外键关系,Book有许多字段,但我最关心的是author(一个标准CharField)。
话虽如此,在我的PersonAdmin模型中,我想显示book。作者使用list_display:
class PersonAdmin(admin.ModelAdmin):
list_display = ['book.author',]
我已经尝试了所有显而易见的方法,但似乎都不起作用。
有什么建议吗?
当前回答
根据文档,你只能显示ForeignKey的__unicode__表示:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#list-display
似乎奇怪的是,它不支持'book__author'风格的格式,这种格式在DB API中随处可见。
事实证明,这个功能有一个门票,它被标记为不会修复。
其他回答
对于Django >= 3.2
在Django 3.2或更高版本中,正确的方法是使用显示装饰器
class BookAdmin(admin.ModelAdmin):
model = Book
list_display = ['title', 'get_author_name']
@admin.display(description='Author Name', ordering='author__name')
def get_author_name(self, obj):
return obj.author.name
根据文档,你只能显示ForeignKey的__unicode__表示:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#list-display
似乎奇怪的是,它不支持'book__author'风格的格式,这种格式在DB API中随处可见。
事实证明,这个功能有一个门票,它被标记为不会修复。
作为另一种选择,你可以像这样查找:
#models.py
class UserAdmin(admin.ModelAdmin):
list_display = (..., 'get_author')
def get_author(self, obj):
return obj.book.author
get_author.short_description = 'Author'
get_author.admin_order_field = 'book__author'
从Django 3.2开始,你可以使用display()装饰器:
#models.py
class UserAdmin(admin.ModelAdmin):
list_display = (..., 'get_author')
@admin.display(ordering='book__author', description='Author')
def get_author(self, obj):
return obj.book.author
请注意,添加get_author函数会减慢管理中的list_display,因为显示每个人会产生SQL查询。
为了避免这种情况,您需要修改PersonAdmin中的get_queryset方法,例如:
def get_queryset(self, request):
return super(PersonAdmin,self).get_queryset(request).select_related('book')
之前:36.02ms内73个查询(管理中67个重复查询) 之后:10.81ms内查询6次
我可能会迟到,但这是另一种方法。你可以简单地在你的模型中定义一个方法,并通过list_display访问它,如下所示:
models.py
class Person(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
def get_book_author(self):
return self.book.author
admin.py
class PersonAdmin(admin.ModelAdmin):
list_display = ('get_book_author',)
但是这种方法和上面提到的其他方法会在列表视图页面的每一行中增加两个额外的查询。为了优化这一点,我们可以覆盖get_queryset来注释所需的字段,然后在ModelAdmin方法中使用注释的字段
admin.py
from django.db.models.expressions import F
@admin.register(models.Person)
class PersonAdmin(admin.ModelAdmin):
list_display = ('get_author',)
def get_queryset(self, request):
queryset = super().get_queryset(request)
queryset = queryset.annotate(
_author = F('book__author')
)
return queryset
@admin.display(ordering='_author', description='Author')
def get_author(self, obj):
return obj._author