Add FormMixin.get_form_kwargs method to abstract some common logic in a tidier

way. Tidy up (and amend) documentation about FormMixin and ModelFormMixin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14865 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Chris Beaven 2010-12-10 03:51:30 +00:00
parent ff8711a825
commit 2e909489d7
2 changed files with 39 additions and 40 deletions

View File

@ -31,16 +31,19 @@ class FormMixin(object):
""" """
Returns an instance of the form to be used in this view. Returns an instance of the form to be used in this view.
""" """
return form_class(**self.get_form_kwargs())
def get_form_kwargs(self):
"""
Returns the keyword arguments for instanciating the form.
"""
kwargs = {'initial': self.get_initial()}
if self.request.method in ('POST', 'PUT'): if self.request.method in ('POST', 'PUT'):
return form_class( kwargs.update({
data=self.request.POST, 'data': self.request.POST,
files=self.request.FILES, 'files': self.request.FILES,
initial=self.get_initial() })
) return kwargs
else:
return form_class(
initial=self.get_initial()
)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
return kwargs return kwargs
@ -75,22 +78,13 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
model = self.get_queryset().model model = self.get_queryset().model
return model_forms.modelform_factory(model) return model_forms.modelform_factory(model)
def get_form(self, form_class): def get_form_kwargs(self):
""" """
Returns a form instantiated with the model instance from get_object(). Returns the keyword arguments for instanciating the form.
""" """
if self.request.method in ('POST', 'PUT'): kwargs = super(ModelFormMixin, self).get_form_kwargs()
return form_class( kwargs.update({'instance': self.object})
data=self.request.POST, return kwargs
files=self.request.FILES,
initial=self.get_initial(),
instance=self.object,
)
else:
return form_class(
initial=self.get_initial(),
instance=self.object,
)
def get_success_url(self): def get_success_url(self):
if self.success_url: if self.success_url:

View File

@ -413,27 +413,34 @@ FormMixin
.. method:: get_initial() .. method:: get_initial()
Retrieve initial data for the form. By default, returns Retrieve initial data for the form. By default, returns
:attr:`FormMixin.initial`. :attr:`.initial`.
.. method:: get_form_class() .. method:: get_form_class()
Retrieve the form class to instantiate. By default, Retrieve the form class to instantiate. By default
:attr:`FormMixin.form_class`. :attr:`.form_class`.
.. method:: get_form(form_class) .. method:: get_form(form_class)
Instantiate an instance of ``form_class``. If the request is a ``POST`` Instantiate an instance of ``form_class`` using
or ``PUT``, the request data (``request.POST`` and ``request.FILES``) :meth:`.get_form_kwargs`.
will be provided to the form at time of construction.
.. method:: get_form_kwargs()
Build the keyword arguments requried to instanciate an the form.
The ``initial`` argument is set to :meth:`.get_initial`. If the
request is a ``POST`` or ``PUT``, the request data (``request.POST``
and ``request.FILES``) will also be provided.
.. method:: get_success_url() .. method:: get_success_url()
Determine the URL to redirect to when the form is successfully Determine the URL to redirect to when the form is successfully
validated. Returns :attr:`FormMixin.success_url` by default. validated. Returns :attr:`.success_url` by default.
.. method:: form_valid() .. method:: form_valid()
Redirects to :attr:`ModelFormMixin.success_url`. Redirects to :meth:`.get_success_url`.
.. method:: form_invalid() .. method:: form_invalid()
@ -449,9 +456,9 @@ FormMixin
.. note:: .. note::
Views mixing :class:`~django.views.generic.edit.FormMixin` must Views mixing :class:`FormMixin` must
provide an implementation of :meth:`~FormMixin.form_valid` and provide an implementation of :meth:`.form_valid` and
:meth:`~FormMixin.form_invalid`. :meth:`.form_invalid`.
ModelFormMixin ModelFormMixin
~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
@ -490,12 +497,10 @@ ModelFormMixin
:attr:`~SingleObjectMixin.model`, depending on which attribute is :attr:`~SingleObjectMixin.model`, depending on which attribute is
provided. provided.
.. method:: get_form(form_class) .. method:: get_form_kwargs()
Instantiate an instance of ``form_class``. If the request is a ``POST`` Add the current instance (``self.object``) to the standard
or ``PUT``, the request data (``request.POST`` and ``request.FILES``) :meth:`FormMixin.get_form_kwargs`.
will be provided to the form at time of construction. The current
instance (``self.object``) will also be provided.
.. method:: get_success_url() .. method:: get_success_url()
@ -506,7 +511,7 @@ ModelFormMixin
.. method:: form_valid() .. method:: form_valid()
Saves the form instance, sets the current object for the view, and Saves the form instance, sets the current object for the view, and
redirects to :attr:`ModelFormMixin.success_url`. redirects to :meth:`.get_success_url`.
.. method:: form_invalid() .. method:: form_invalid()