#27231: Initialize forms in ModelAdmin like View (i.e. add get_form_kwargs to
contrib.admin)
----------------------------------+--------------------
     Reporter:  thauk-copperleaf  |      Owner:  nobody
         Type:  Uncategorized     |     Status:  new
    Component:  contrib.admin     |    Version:  1.10
     Severity:  Normal            |   Keywords:
 Triage Stage:  Unreviewed        |  Has patch:  0
Easy pickings:  0                 |      UI/UX:  0
----------------------------------+--------------------
 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
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27231>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/059.20fdd572c16038bd7ce4b0f37d4877a2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to