Show inline model field in list_display
我有两种型号:
1 2 3 4 5 | class Person(models.Model): person_name = models.CharField(max_length=255) def __unicode__(self): return self.person_name |
和
1 2 3 4 5 6 7 8 | class Book(models.Model): link = models.ForeignKey(Person) book_name = models.CharField(max_length=255) book_year = models.CharField(max_length=255) book_email = models.CharField(max_length=255) def __unicode__(self): return self.name |
号
行政副总裁
1 2 3 4 5 6 7 8 | class PersonAdmin(admin.ModelAdmin): inlines = ('BookInline',) list_display = ('person_name', ...) class BookInline(admin.TabularInline): model = Book extra = 1 max_num = 1 |
在
所以当我访问django admin中的
把这个人的名字写在书的
1 2 3 4 5 6 7 8 | class BookAdmin(admin.ModelAdmin): inlines = ('BookInline',) list_display = ('person_name', 'book_name', 'book_email', 'book_year') def person_name(self, obj): returns obj.link.person_name admin.site.register(Book, BookAdmin) |
1 2 3 4 | class BookInline(admin.StackedInline): model = Book extra = 1 max_num = 1 |
号
叠加将解决您的问题
根据文档,您可以使用Callable来显示字段。