AndrewIngram/django-extra-views

How to use two formset in extra-views?

sangramrajekakade opened this issue · 1 comments

I am working on style properties and style option form both are different formset how I manage two formset in one view i am trying with this view but

Calling modelformset_factory without defining 'fields' or 'exclude' explicitly is prohibited.
this error show


class StylePropsUpdateViews(ModelFormSetView):
    model = StyleProps
    prop_form= StylePropsForm
    option_form = StylesOptionForm
    template_name="master/styleprops_form.html"
    def get_queryset(self):
        pk = self.kwargs.get('pk')
        current_user = self.request.user
        return self.model.objects.filter(ProductName=pk, user=current_user)

    def get_success_url(self):
        return reverse("tailoringproducts")

    def formset_valid(self, prop_form, option_form):
    	  for docs_form in prop_form:
    	  	  docs_form.save(commit=False)
    	  	  docs_form.instance.ProductName_id = self.kwargs.get('pk')
    	  	  docs_form.instance.user = self.request.user
    	  	  docs_form.save()
    	  	  messages.success(self.request, "Styling Properties Updated successfully")
    	  	  return HttpResponseRedirect(self.get_success_url())

    def formset_invalid(self, prop_form, option_form):
        messages.error(self.request, "Form getting invalid")
        return self.render_to_response(self.get_context_data(prop_form=prop_form, option_form=option_form))

see in this image I have option button after clicking on the options button option form will open

Screenshot from 2019-06-18 17-11-32

Screenshot from 2019-06-18 17-11-43

each style option form row has its own option form

can anyone help me in this

The probable reason for the error is that you did not define fields, exclude or a form_class which had fields/exclude set in it's meta when you defined StylePropsUpdateViews. The below might have fixed that. I cannot help with the other problems as it looks like you are using a bespoke setup with multiple forms per object in the queryset.

class StylePropsUpdateViews(ModelFormSetView):
    model = StyleProps
    form_class = StylePropsForm
    ...