user2552806
user2552806

Reputation:

Django add inlines to CreateView

I have the following 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)

And the following 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

And the following views.py:

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

But it is not working with 'a' inside "fields = ['name', 'a']", as 'a' is not found.

How can I get inlines into the view so I could edit/delete/create A inside B view?

Upvotes: 1

Views: 183

Answers (1)

Alasdair
Alasdair

Reputation: 308899

The CreateView does not support this. You could use django-extra-views, which comes with CreateWithInlinesView and UpdateWithInlinesView views.

Upvotes: 1

Related Questions