Django Admin:缩进模型父外键自我关系

时间:2017-04-26 19:08:19

标签: django python-3.x django-models django-admin

这是我的模特:

class Company(models.Model):
    class Meta:
        verbose_name_plural = "companies"

    name = models.CharField(max_length=30)
    parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL)

    def __str__(self):
        return 'Company: %s' % self.name

是否有办法修改def __str__方法以显示具有-或缩进的子项,具体取决于与其父级的关系?像这样:

Company A
- Company C
Company B
- Company D
- Company E
-- Company F

0 个答案:

没有答案