Opened 3 hours ago

#35949 new New feature

Add formset_kwargs keyword argument to BaseFormSet

Reported by: Richard Brockie Owned by:
Component: Forms Version: 5.1
Severity: Normal Keywords: formset validation
Cc: Richard Brockie Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

I find it useful when validating formsets to sometimes define and set an attribute that can be used during validation.

class MyFormSet(BaseFormSet):
    useful_attribute = None

    def clean(self):
        super().clean()

        # validation code using self.useful_attribute...


# usage in a view...

    the_formset = formset_factory(MyForm, formset=MyFormSet, extra=0)

    bound_formset = the_formset(initial=initial_results,
    	form_kwargs=form_kwargs)

    bound_formset.useful_attribute = useful_value

I'm wondering if formset_kwargs could be added to BaseFormSet to follow the form_kwargs pattern. The suggested usage would be something like this:

class MyFormSet(BaseFormSet):
    def __init__(self, useful_attribute, *args, **kwargs):
        self.useful_attribute = useful_attribute
        super().__init__(*args, **kwargs)

    def clean(self):
        super().clean()

        # validation code using self.useful_attribute...


# usage in a view...

    the_formset = formset_factory(MyForm, formset=MyFormSet, extra=0)

    bound_formset = the_formset(initial=initial_results,
    	form_kwargs=form_kwargs,
    	formset_kwargs={"useful_attribute": useful_value})

The kwargs provided in formset_kwargs would then be passed to MyFormSet when it is instantiated.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top