How to display an attribute of a foreign key in the Django admin page

前端 未结 3 451
独厮守ぢ
独厮守ぢ 2021-01-18 17:34

I want to display the level field of the category to which the product is related on the object\'s admin page.

    class Category(m         


        
3条回答
  •  情书的邮戳
    2021-01-18 17:52

    The simplest way is to put the level of the Category into the __unicode__ method:

    class Category(models.Model):
        name = models.CharField(max_length=50, default=False)
        level = models.IntegerField(help_text="1, 2 ,3 or 4")
    
        def __unicode__(self):
            return u'%s [%d]' % (self.name, self.level)
    

    So the select box will show it.

提交回复
热议问题