﻿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
27231	Initialize forms in ModelAdmin like View (i.e. add get_form_kwargs to contrib.admin)	Thomas Hauk	nobody	"There's no way to control initialization of a form instance with ModelAdmin like you can with, say, a View, by providing an override for get_form_kwargs. This is something I need to do.

Is there a architectural reason why ModelAdmin doesn't have get_form_kwargs? Or if I provided a PR, would that be welcome?

The closest ticket I can find related to this is #10305.

This is kind of how I'm working around it for now... to get one particular kind of value into my form constructor call, I'm kind of ""injecting"" it in:


{{{
class MyModelAdmin(ModelAdmin):
    model = MyModel
    form = MyForm
 
    def get_form(self, request, obj=None, **kwargs):
        form_class = super(MyModelAdmin, self).get_form(request, obj=obj, **kwargs)
 
        some_parameter_for_one_thing = self.get_some_parameter_for_one_thing()
 
        from django.forms.models import ModelFormMetaclass
        class FormMetaclass(ModelFormMetaclass):
            def __new__(mcs, name, bases, attrs):
                attrs[""some_parameter_for_one_thing""] = some_parameter_for_one_thing
                return super(FormMetaclass, mcs).__new__(mcs, name, bases, attrs)
 
        another_parameter_for_another_thing= obj.another_parameter_for_another_thing
 
        import six
        class FormWrapper(six.with_metaclass(FormMetaclass, form_class)):
            def __init__(self, *args, **kwargs):
                data = {""another_parameter_for_another_thing"": six.text_type(another_parameter_for_another_thing)}
                super(FormWrapper, self).__init__(data, *args, **kwargs)
 
        return FormWrapper
}}}
"	New feature	closed	contrib.admin	1.10	Normal	wontfix			Unreviewed	0	0	0	0	0	0
