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

前端 未结 7 1082
有刺的猬
有刺的猬 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 19:01

    I just added my own version after checking out some of those pre-made CBVs. I specifically needed control over multiple formsets -> one parent in a single view each with individual save functions.

    I basically stuffed the FormSet data binding into a get_named_formsets function which is called by get_context_data and form_valid.

    There, I check if all formsets are valid, and also look for a method that overrides a plain old formset.save() on a per formset basis for custom saving.

    The template renders formsets via

    {% with named_formsets.my_specific_formset as formset %}
     {{ formset }}
     {{ formset.management_form }}
    {% endwith %}
    

    I think I'll be using this system regularly.

    class MyView(UpdateView): # FormView, CreateView, etc
        def get_context_data(self, **kwargs):
            ctx = super(MyView, self).get_context_data(**kwargs)
            ctx['named_formsets'] = self.get_named_formsets()
            return ctx
    
        def get_named_formsets(self):
            return {
                'followup': FollowUpFormSet(self.request.POST or None, prefix='followup'),
                'action': ActionFormSet(self.request.POST or None, prefix='action'),
            }
    
        def form_valid(self, form):
            named_formsets = self.get_named_formsets()
            if not all((x.is_valid() for x in named_formsets.values())):
                return self.render_to_response(self.get_context_data(form=form))
    
            self.object = form.save()
    
            # for every formset, attempt to find a specific formset save function
            # otherwise, just save.
            for name, formset in named_formsets.items():
                formset_save_func = getattr(self, 'formset_{0}_valid'.format(name), None)
                if formset_save_func is not None:
                    formset_save_func(formset)
                else:
                    formset.save()
            return http.HttpResponseRedirect('')
    
        def formset_followup_valid(self, formset):
            """
            Hook for custom formset saving.. useful if you have multiple formsets
            """
            followups = formset.save(commit=False) # self.save_formset(formset, contact)
            for followup in followups:
                followup.who = self.request.user
                followup.contact = self.object
                followup.save()
    

提交回复
热议问题