Django为CreateView添加了内联

时间:2016-07-04 14:55:06

标签: django django-models django-views

我有以下admin.py

class AInlineAdmin(admin.TabularInline):
    model = A

class BAdmin(admin.ModelAdmin):
    fields = ['name']
    list_display = ['name']
    ordering = ['name']
    inlines = [AInlineAdmin]

admin.site.register(B, BAdmin)

class AAdmin(admin.ModelAdmin):
    fields = ['identifier']
    list_display = ['identifier']
    ordering = ['identifier']

admin.site.register(A, AAdmin)

以下models.py:

class B(models.Model):
    name = models.CharField(max_length=100)

    def get_A(self):
        return "\n".join([i.identifier for i in self.a.all()])

    def __unicode__(self):
        return self.name

class A(models.Model):
    identifier = models.CharField(max_length=200, blank=False, default="")
    c = models.ForeignKey(B, related_name='a', default=0)

    def __unicode__(self):
        return self.identifier

以下views.py:

class BCreate(CreateView):
    model = B
    fields = ['name', 'a']

但它没有使用'fields'['name','a']“中的'a',因为找不到'a'。

如何在视图中插入内联,以便编辑/删除/创建B视图内部?

1 个答案:

答案 0 :(得分:2)

CreateView不支持此功能。你可以使用django-extra-views,它带有CreateWithInlinesView and UpdateWithInlinesView个视图。