django class-based views with inline model-form or formset

Key points is: generated FormSets within forms.py using inlineformset_factory: BookImageFormSet = inlineformset_factory(BookForm, BookImage, extra=2) BookPageFormSet = inlineformset_factory(BookForm, BookPage, extra=5) returned the FormSets within a CreateView class in views.py: def get_context_data(self, **kwargs): context = super(BookCreateView, self).get_context_data(**kwargs) if self.request.POST: context[‘bookimage_form’] = BookImageFormSet(self.request.POST) context[‘bookpage_form’] = BookPageFormSet(self.request.POST) else: context[‘bookimage_form’] = BookImageFormSet() context[‘bookpage_form’] = BookPageFormSet() return context Used form_valid to … Read more

Creating a model and related models with Inline formsets

First, create a Author model form. author_form = AuthorModelForm() then create a dummy author object: author = Author() Then create a inline formset using the dummy author like so: formset = BookFormSet(instance=author) #since author is empty, this formset will just be empty forms Send that off to a template. After the data is returned back … Read more