欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

django admin显示多对多字段ManyToManyField

程序员文章站 2022-11-09 09:23:16
参考文档https://jingyan.baidu.com/article/4e5b3e190f55c591901e24b3.html admin.py from .models import *class BookAdmin(admin.ModelAdmin): list_display = [" ......

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

 

admin.py

django admin显示多对多字段ManyToManyField
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

 

models.py

django admin显示多对多字段ManyToManyField
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

 django admin显示多对多字段ManyToManyField