多对多作为内联

时间:2018-07-30 01:50:10

标签: django django-admin

在ModelAdmin中尝试将ManyToMany关系用作内联时,在底部导致错误admin.E105,但在正常情况下,如果没有内联,则可以正常工作。这是代码:

models.py

class Reference(models.Model):
    url = models.UrlField()
    ...


class BaseModel(models.Model):
    ...
    references = models.ManyToManyField(
            Reference,
            related_name="references_%(app_label)s_%(class)s_related",
        )

    class Meta:
        abstract=True


class Case(BaseModel)
    ...
    name = models.CharField(max_length=255)
    ...

admin.py

class InlineReference(admin.TabularInline):
    model = BaseModel.references.through


class CaseAdmin(admin.ModelAdmin):
    ...
    inlines = [InlineReference, ]
    exclude = ['references', ]
    ...

错误消息是:

SystemCheckError: System check identified some issues:
ERRORS:
<class 'admin.CaseAdmin'>: (admin.E105) 'admin.InlineReference' must have a 'model' attribute.

我正在尝试关注here中的文档。

1 个答案:

答案 0 :(得分:-1)

您必须设置模型属性参考模型。

class InlineReference(admin.StackedInline):
    model = Reference


class BaseAdmin(admin.ModelAdmin):
    ...
    inlines = [InlineReference, ]
    ...

admin.site.register(Base, BaseAdmin)