django admin显示多对多字段ManyToManyField
参考文档https://jingyan.baidu.com/article/4e5b3e190f55c591901e24b3.html
admin.py
from .models import *
class BookAdmin(admin.ModelAdmin): list_display = ["title","作者"] def 作者(self, obj): return [bt.name for bt in obj.authors.all()] filter_horizontal = ('authors',) admin.site.register(Book,BookAdmin)
models.py
class Book(models.Model): title = models.CharField(max_length=32) authors = models.ManyToManyField("Author") def __str__(self): return self.title
class Author(models.Model):
name = models.CharField(max_length=32)
def __str__(self):
return self.name
作者:子钦加油
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年