参考:
https://www.ywcsb.vip/blog/101.html
list_display展示外键内容
表结构关系
表一:
class Person(models.Model):firstname = models.CharField(maxlength=50)surname = models.CharField(maxlength=50)
表二
class Friends(models.Model):person1 = models.ForeignKey("Person")friendship_made = models.DateField()
admin.py
admin中可以写如下内容class PersonAdmin(admin.ModelAdmin):list_display = ["firstname", "surname", "friendship_made"] # 这里需要注意的是,firstname、surname是我们要在前端展示的内容,自己定义的def firstname(self, obj):return obj.person1.firstname # person1是我们Person表中外键的字段,firstname是刚才我们自定义的字段,firstname.short_description = 'Review' # 对firstname这个做个简短的title。def surname(self, obj):return obj.person1.surnamesurname.short_description = 'Review'