Opened 8 hours ago

Closed 4 hours ago

#35949 closed New feature (wontfix)

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, David Smith 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 (1)

comment:1 by Sarah Boyce, 4 hours ago

Cc: David Smith added
Resolution: wontfix
Status: newclosed

Hi Richard, thank you for the ticket

For new feature requests, the recommended path forward is to first propose and discuss the idea with the community and gain consensus. To do that, please consider starting a new conversation on the Django Forum, where you'll reach a broader audience and receive additional feedback.

I'll close the ticket for now, but if the community agrees with the proposal, please return to this ticket and reference the forum discussion so we can re-open it. For more information, please refer to the documented guidelines for requesting features.

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