Dynamic Creation of Forms with master-detail models?

I would like one form to deal with adding new Articles and on the same page the capability to add sections to the Article model (which link to the newly created Article).

class Section(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE);
    content = RichTextField(max_length=1000)

In other words, each article can have multiple sections attached to it.

I have rigged up some HTML and Jquery which allows me to add/delete sections.

When I add Section, I get this :

Now I can delete Sections…So I can delete section 2, and Section 3 will become Section 2…

hope this makes sense :smile:

How do I go about converting this idea to Django? I haven’t seen any example of a master-detail relationship on one-page. They always seem to go to another page to add in the detail sections.

git hub code : https://github.com/squeezemylizard/jquery_stuff

My JQuery isn’t pretty - so if you have better ways to do this – I’m all ears.

If anyone is interested, I’ve made progress using an inline form set

class ArticleCreateView(LoginRequiredMixin,CreateView):
    template_name = 'articles/article_add.html'
    model = Article
    form_class = ArticleForm
    success_url = reverse_lazy('index')
    SectionFormSet = inlineformset_factory(Article, Section, extra=1, fields=('content',))
    
    def get(self, request, *args, **kwargs):
        print('get called on article create view')
        self.object = None   #what does this achieve?
        
        return self.render_to_response(
            self.get_context_data(form=self.get_form(),
                                  section_form=self.SectionFormSet(),
                                  ))

    def post(self, request, *args, **kwargs):
        print('post called on article create view')
        self.object = None
     
        form = self.get_form()
        section_form = self.SectionFormSet(self.request.POST)
        
        if (form.is_valid() and section_form.is_valid()):
            return self.form_valid(form, section_form)
        else:
            return self.form_invalid(form, section_form)

    
    def form_valid(self, form, section_form):
        form.instance.author = self.request.user
        if section_form.is_valid():
            self.object = form.save()
            section_form.instance = self.object
            section_form.save()
            
        #return super().form_valid(form)
        
        return self.render_to_response( self.get_context_data(form=form,
                                  section_form=section_form,
                                  ))
        
    

    def form_invalid(self, form, article_form):
        return self.render_to_response(
            self.get_context_data(form=form,
                                  article_form=article_form))

It basically works for one inline form for section.

However, there are issues . If you look at the last couple of lines

        
        return self.render_to_response( self.get_context_data(form=form,
                                  section_form=section_form,
                                  ))

If I use the top line it redirects to success url. BUT… does this

 saving article model
 saving section model
 saving article model

using the 2nd line redirects back to where you’ve just added the article – which is not what I want but only save the article model once…

sigh.

I’ve kind of cobbled this together from various other forums, and a little bit confused if I’m doing it right. There’s code in here I’m slightly suspicious about to say the least.