Unable to delete form/instance from inlineformset

41 views
Skip to first unread message

Luboš Truhlář

unread,
Sep 23, 2016, 7:58:50 AM9/23/16
to Django users

Using Django 1.9.9, Python 2.7


I am trying to figure out why I am not able to delete form/instance inside of FormSet. I was folowing debug to this function in django.forms.formsets.py line 300.

def _should_delete_form(self, form):
       """
       Returns whether or not the form was marked for deletion.
       """
       return form.cleaned_data.get(DELETION_FIELD_NAME, False)

In my case:

form.cleaned_data = {'name': 2, 'file': FieldFile: real_estate_1/podpis.html}
data = { u'contracts-0-DELETE': [u'on'], ...}
changed_data = [[u'DELETE'], ...]

... The main problem is that cleaned_data missing the DELETE field. The formset has self.can_delete = True. But I am not able to figure out why the field is missing there...?


I would appreciate any advice.

My classes look this:

class DocumentsForm(CustomModelForm):
    file = forms.FileField(label=_(u'Smlouva'), required=True)
    empty_permitted = False

class Meta:
    model = Contract
    fields = ['name', 'file']
    fields_with_permissions = ['file']


class RequestInlineFormSet(BaseGenericInlineFormSet):
    formset = 'contracts'
    empty_permitted = False

    def __init__(self, *args, **kwargs):
       self.request = kwargs.pop('request', None)
       form_kwargs = {'request': self.request}
       super(RequestInlineFormSet, self).__init__(form_kwargs=form_kwargs, *args, **kwargs)


ContractFormSet = generic_inlineformset_factory(Contract,
                                            form=DocumentsForm,
                                            formset=RequestInlineFormSet,
                                            can_order=False,
                                            can_delete=True,
                                            fields=('name', 'file'),
                                            extra=1)
Reply all
Reply to author
Forward
0 new messages