子钦加油

扩大
缩小

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
复制代码

 

posted on 2019-12-15 23:50  子钦加油  阅读(1641)  评论(0编辑  收藏  举报

导航

返回顶部