首页 > 解决方案 > 空的 Django 内联说字段是必需的

问题描述

在 django admin 中,我正在尝试创建一个新条目,但内联表单中的字段会抛出错误,说即使我不想创建新的内联,它们也是必需的。

楷模:

class Attribute(models.Model):
    name = models.CharField(choices=ATTRIBUTE_CHOICES, max_length=max_choice_len(ATTRIBUTE_CHOICES))
    amount = models.PositiveIntegerField()

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()

    def __str__(self):
        return f'{self.name} {str(self.amount)}'

class Item(models.Model):
    ...
    attributes = GenericRelation(Attribute)

行政:

class AttributeInline(GenericTabularInline):
    model = Attribute
    extra = 0

class ItemAdmin(admin.ModelAdmin):
    inlines = [AttributeInline]
    class Meta:
        model = Item

admin.site.register(Item, ItemAdmin)

标签: djangoadmininline

解决方案


推荐阅读