﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25408	Pass additional arguments to BaseForm.__init__ from BaseModelForm.__init__	Moritz Sichert	nobody	"Consider the following module ""myapp.forms"":

{{{
class CustomForm(BaseForm):
    def __init__(self, **kwargs):
        self.custom_kwarg = kwargs.pop('custom_kwarg', 'not available')

    def is_valid(self):
        if self.custom_kwarg:
            return True
        else:
            return super().is_valid()


class CustomModelForm(ModelForm, CustomForm):
    pass
}}}
This module adds additional functionality to forms by adding a new keyword argument. If you want to use the new feature on a ModelForm too, that won't work because `BaseModelForm.__init__()` doesn't capture additional arguments.
You can work around that like follows:

{{{
class CustomFormArg(BaseForm):
    def __init__(self, **kwargs):
        self.custom_kwarg = kwargs.pop('custom_kwarg', False)


class CustomFormMethod(BaseForm):
    def is_valid(self):
        if self.custom_kwarg:
            return True
        else:
            return super().is_valid()


class CustomForm(CustomFormArg, CustomFormMethod):
    pass


class CustomModelForm(CustomFormArg, ModelForm, CustomFormMethod):
    pass
}}}
But I think it makes more sense to let BaseModelForm pass the arguments to the super() call. Maybe it would also be better if BaseModelForm only took `**kwargs` instead of listing all arguments again.

Note: In some cases this can be fixed by reversing the bases of CustomModelForm:
{{{
class CustomModelForm(CustomForm, ModelForm):
    pass
}}}
However then the mro would be:
{{{
myapp.forms.CustomModelForm
myapp.forms.CustomForm
django.forms.models.ModelForm
django.forms.models.BaseModelForm
django.forms.forms.BaseForm
object
}}}
instead of the more correct
{{{
myapp.forms.CustomModelForm
django.forms.models.ModelForm
django.forms.models.BaseModelForm
myapp.forms.CustomForm
django.forms.forms.BaseForm
object
}}}"	Cleanup/optimization	closed	Forms	dev	Normal	needsinfo	form modelform args		Accepted	0	0	0	0	0	0
