通过ajax调用一次保存一个表单集

时间:2019-04-25 10:10:52

标签: django django-models django-forms django-views

我要逐行保存表单集数据,当我提交表单时,表单处于有效状态,

在“发布请求”中,属性名称为form-1-Name,这就是表单无效的原因。

@method_decorator(login_required, name='dispatch')
class ProductionProcessCreateView(CreateView):
    model = ProductionProcess
    form_class = ProductionProcessForm
    template_name="master/productionprocess_form.html"
    ExampleFormSet = modelformset_factory(ProductionProcess,form=form_class,formset=BaseProductionProcessFormSet,extra=1,)


    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        context = super(ProductionProcessCreateView, self).get_context_data(**kwargs)
        if self.request.POST:
            context['ProductionProcessForm'] = self.ExampleFormSet(self.request.POST)

        else:
            context['ProductionProcessForm'] = self.ExampleFormSet()        
        return self.render_to_response(context)


    def post(self, request, *args, **kwargs):

        try:  
            ExampleFormSet = self.ExampleFormSet(self.request.POST)

        except ValidationError:
            ExampleFormSet = None

        self.object = self.get_object()
        if ExampleFormSet.is_valid():


            for form in ExampleFormSet.save(commit=False):
                form.user = self.get_object().user
                form.instance.ProductName_id = self.kwargs.get('pk')
                form.save()
            messages.success(self.request, _('Production Process added successfully'))
            return redirect("tailoringproducts")
        else:


            return self.render_to_response(self.get_context_data(ExampleFormSet=ExampleFormSet))

class BaseProductionProcessFormSet(BaseModelFormSet):
    def __init__(self,prefix=None,*args,**kwargs):
        super(BaseProductionProcessFormSet, self).__init__(*args, **kwargs)
        self.queryset = ProductionProcess.objects.filter(ProductName_id=1)

Expected 每行都有保存按钮,当单击保存按钮时,数据将保存在数据库中,我们如何通过ajax调用来存储数据

0 个答案:

没有答案