参考文档https://jingyan.baidu.com/article/4e5b3e190f55c591901e24b3.html

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

admin.py

django admin显示多对多字段ManyToManyField Python 第1张
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)
django admin显示多对多字段ManyToManyField Python 第2张

 

models.py

django admin显示多对多字段ManyToManyField Python 第3张
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
django admin显示多对多字段ManyToManyField Python 第4张

 django admin显示多对多字段ManyToManyField Python 第5张

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄