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

前端 未结 7 1108
有刺的猬
有刺的猬 2020-11-28 18:08

I have the following models:

class Bill(models.Model):
    date = models.DateTimeField(_(\"Date of bill\"),null=True,blank=True)

class Item(models.Model):
          


        
7条回答
  •  伪装坚强ぢ
    2020-11-28 18:50

    Key points is:

    1. generated FormSets within forms.py using inlineformset_factory:

      BookImageFormSet = inlineformset_factory(BookForm, BookImage, extra=2)
      BookPageFormSet = inlineformset_factory(BookForm, BookPage, extra=5)
      
    2. 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
      
    3. Used form_valid to save the form and formset:

       def form_valid(self, form):
           context = self.get_context_data()
           bookimage_form = context['bookimage_formset']
           bookpage_form = context['bookpage_formset']
           if bookimage_form.is_valid() and bookpage_form.is_valid():
               self.object = form.save()
               bookimage_form.instance = self.object
               bookimage_form.save()
               bookpage_form.instance = self.object
               bookpage_form.save()
               return HttpResponseRedirect('thanks/')
           else:
               return self.render_to_response(self.get_context_data(form=form))
      

提交回复
热议问题