Django - Include data from foreignkey in admin list_display function

前端 未结 3 1072
野的像风
野的像风 2021-01-20 03:48

I have two models and one admin model:

class Person(models.Model):

    firstname = models.CharField(maxlength=50)
    surname = models.CharField(maxlength=5         


        
3条回答
  •  心在旅途
    2021-01-20 04:11

    This code isn't complete - you've got two ForeignKeys from Friends to Person, so at least one of them will need a related_name attribute to prevent clashes. The code simply won't run as it is.

    If a Person can only be friends with one other person, you should use a OneToOneField rather than a ForeignKey. Then, you can simply refer to my_person.friend1.person1.surname, where my_person is the Person you starting with, and friend1 is the related_name value I mentioned above.

提交回复
热议问题