djblets.webapi.resources.mixins.forms¶
Mixins for integrating a web API resource with a form.
-
class
UpdateFormMixin[source]¶ Bases:
objectA mixin for providing the ability to create and update using a form.
A WebAPIResource class using this mixin must set the
form_classattribute to aModelForminstance that corresponds to the model being updated.Classes using this mixin can provide methods of the form
parse_<field_name>_fieldto do parsing of form data before it is passed to the form. Parser methods should be of the form:def parse_some_field(self, value, request, **kwargs): # ...
These methods may return either a single value or a list of values (in the case where the corresponding field expects a list of values, such as a
django.forms.ModelMultipleChoiceField).The
create_form()methods should be used to create new form instances. A form created this way can be given an optional instance argument to allow for updating the instance. Any fields missing from data (but appearing in theform_class’sfieldsattribute) will be copied over from the instance.-
create_form(data, request, instance=None, **kwargs)[source]¶ Create a new form and pre-fill it with data.
Parameters: - data (dict) – The request data to pass to the form.
- request (django.http.HttpRequest) – The HTTP request.
- instance (django.db.models.Model) – The instance model, if it exists. If this is not
None, fields that appear in the form class’sfieldsattribute that do not appear in thedatadict as keys will be copied from the instance. - **kwargs (dict) – Additional arguments. These will be passed to the resource’s parser methods.
Returns: The form with data filled.
Return type:
-