Ticket #12755: modeladmin_get_form_instance.patch

File modeladmin_get_form_instance.patch, 2.5 KB (added by Igor Sobreira, 15 years ago)

Add get_form_instance() method to ModelAdmin

  • django/contrib/admin/options.py

     
    363363        defaults.update(kwargs)
    364364        return modelform_factory(self.model, **defaults)
    365365
     366    def get_form_instance(self, request, form_class, **kwargs):
     367        """
     368        Returns a Form instance for use in the admin ``add_view`` and ``change_view``. The
     369        ``form_class`` parameter is returned by ``get_form()``
     370        """
     371        return form_class(**kwargs)
     372
    366373    def get_changelist(self, request, **kwargs):
    367374        """
    368375        Returns the ChangeList class for use on the changelist page.
     
    767774        ModelForm = self.get_form(request)
    768775        formsets = []
    769776        if request.method == 'POST':
    770             form = ModelForm(request.POST, request.FILES)
     777            form = self.get_form_instance(request, ModelForm, data=request.POST, files=request.FILES)
    771778            if form.is_valid():
    772779                new_object = self.save_form(request, form, change=False)
    773780                form_validated = True
     
    804811                    continue
    805812                if isinstance(f, models.ManyToManyField):
    806813                    initial[k] = initial[k].split(",")
    807             form = ModelForm(initial=initial)
     814            form = self.get_form_instance(request, ModelForm, initial=initial)
    808815            prefixes = {}
    809816            for FormSet, inline in zip(self.get_formsets(request),
    810817                                       self.inline_instances):
     
    865872        ModelForm = self.get_form(request, obj)
    866873        formsets = []
    867874        if request.method == 'POST':
    868             form = ModelForm(request.POST, request.FILES, instance=obj)
     875            form = self.get_form_instance(request, ModelForm, data=request.POST,
     876                                          files=request.FILES, instance=obj)
    869877            if form.is_valid():
    870878                form_validated = True
    871879                new_object = self.save_form(request, form, change=True)
     
    896904                return self.response_change(request, new_object)
    897905
    898906        else:
    899             form = ModelForm(instance=obj)
     907            form = self.get_form_instance(request, ModelForm, instance=obj)
    900908            prefixes = {}
    901909            for FormSet, inline in zip(self.get_formsets(request, obj), self.inline_instances):
    902910                prefix = FormSet.get_default_prefix()
Back to Top