Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Remove Row in django-extra-views?

In ModelFormSetView how to delete row

here is my code how can I manage delete row in Django-extra-views

I am trying with ModelFormSetView

if formset.deleted_forms:
    for obj in formset.deleted_forms:
        obj.delete()

Html

{{ formset.management_form }}
{% for object in formset %}
{% for hidden in formset.hidden_fields %}
   {{ hidden }}
   {% endfor %}
<tr class="formset"  class="even pointer">{{ object.id }}
   <td class=" ">{{ object.as_p }}</td>                         
   <td class=""></td>
</tr>
    {% endfor %}

View

this is general view how can I manage DELETE filled in this


class MeasurementPropsUpdateViews(ModelFormSetView):
    model = MeasurementProps
    form_class= MeasurementPropsForm

    template_name = "master/measurementprops_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, formset):
            for docs_form in formset:
                docs_form.save(commit=False)
                if formset.deleted_forms:
                    for obj in formset.deleted_forms:
                        obj.delete()
                docs_form.instance.ProductName_id = self.kwargs.get('pk')
                docs_form.instance.user = self.request.user
                docs_form.save()
            messages.success(self.request, "Measurement Properties Updated successfully")
            return HttpResponseRedirect(self.get_success_url())

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



like image 585
InjectorX Avatar asked Oct 28 '22 12:10

InjectorX


1 Answers

I have not tested this out, but according to the documentation, formset-related attributes can be set at the class level. So, in your case, add the factory_kwargs and specify can_delete as True, e.g.:

class MeasurementPropsUpdateView(ModelFormSetView):
    factory_kwargs = { 'can_order': False, 'can_delete': True}

Documentation

like image 140
gregory Avatar answered Oct 31 '22 08:10

gregory