显示嵌套模型的InlineModelAdmin

时间:2018-08-17 09:32:30

标签: python django django-admin

我具有带有OneToOneFields的嵌套模型,并且希望在一个ModelAdmin中具有一个InlineModelAdmin表单以指向嵌套模型...

models.py:

class User(models.Model):
    username = models.CharField(max_length=128)
    password = models.charField(max_length=128)

class IdentityProof(models.Model):
    user = models.OneToOneField(User, on_delete=models.PROTECT, related_name='proof')
    proof_identity = models.FileField(upload_to='uploads/%Y/%m/%d/')

class Company(models.Model):
    user = models.OneToOneField(User, on_delete=models.PROTECT, related_name='company')
    name = models.CharField(max_length=128)

class Person(models.Model):
    user = models.OneToOneField(User, on_delete=models.PROTECT, related_name='person')
    name = models.CharField(max_length=128)

admin.py:

class IdentityProofInline(admin.TabularInline):
    model = IdentityProof

@admin.register(Company)
class CompanyAdmin(admin.ModelAdmin):
    inlines = [IdentityProofInline]

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    inlines = [IdentityProofInline]

对于CompanyAdmin或PersonAdmin,我想显示其用户的IdentityProof。我该怎么办?

我尝试使用fk_name = 'user_proof或其他组合,但不起作用...

谢谢。

1 个答案:

答案 0 :(得分:0)

没有外部包装是不可能的。

参见Django Nested InlineDjango Nested Admin