参考文档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

 django admin显示多对多字段

 

相关文章:

  • 2021-08-15
  • 2021-04-27
  • 2022-12-23
  • 2022-03-02
  • 2022-03-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-31
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-10-16
  • 2021-10-31
  • 2022-01-03
相关资源
相似解决方案