Mixins for integrating a web API resource with a form.
A mixin for providing the ability to create and update using a form.
A WebAPIResource class using this mixin must set the
form_classattribute to a
ModelForminstance 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
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 the
fieldsattribute) will be copied over from the instance.
The form class. This should be a subclass of
create_form(data, request, instance=None, **kwargs)¶
Create a new form and pre-fill it with data.
- 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’s
fieldsattribute that do not appear in the
datadict as keys will be copied from the instance.
- **kwargs (dict) – Additional arguments. These will be passed to the resource’s parser methods.
The form with data filled.