我有一个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',]
我已经尝试了所有显而易见的方法,但似乎都不起作用。
有什么建议吗?
当前回答
在PyPI中有一个非常容易使用的包可以处理这个问题:django-related-admin。你也可以在GitHub中看到代码。
使用它,你想要达到的效果很简单:
class PersonAdmin(RelatedFieldAdmin):
list_display = ['book__author',]
这两个链接都包含了安装和使用的全部细节,所以我不会把它们粘贴在这里,以防它们发生变化。
顺便说一句,如果你已经在使用模型以外的东西。Admin(例如,我使用的是SimpleHistoryAdmin代替),你可以这样做:类MyAdmin(SimpleHistoryAdmin, RelatedFieldAdmin)。
其他回答
如果你尝试在内联,你不会成功,除非:
在内联中:
class AddInline(admin.TabularInline):
readonly_fields = ['localname',]
model = MyModel
fields = ('localname',)
在你的模型(MyModel):
class MyModel(models.Model):
localization = models.ForeignKey(Localizations)
def localname(self):
return self.localization.name
请注意,添加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次
你可以使用可调用对象在列表显示中显示你想要的任何东西。它看起来是这样的:
def book_author(object): return object.book.author class PersonAdmin(admin.ModelAdmin): list_display = [book_author,]
作为另一种选择,你可以像这样查找:
#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
我更喜欢这个:
class CoolAdmin(admin.ModelAdmin):
list_display = ('pk', 'submodel__field')
@staticmethod
def submodel__field(obj):
return obj.submodel.field