Add a mixin for updating models with a ModelForm
Review Request #7494 — Created July 6, 2015 and submitted
The
UpdateFormMixin
is a mixin for aWebAPIResource
that allows a
resource to specify a form class (which should be a subclass of
django.forms.ModelForm
) to use for creating and updating of model
instances. This allows the resource that are very basic wrappers around
models to have their logic further simplified.This mixin is required for allowing partial updating of models.
Traditionally, aModelForm
can take aninstance
parameter
specifying an already existing model instance to update, but will still
require all required field to specified in the form data. This mixin
takes care of that by extracting missing form data from the instance
and inserting it into the form data in the proper format (such as
setting it to be a list of primary keys in the case of a
ModelMultipleChoiceField
).This mixin also allows resources to specify special functions for
parsing values out of the recieved form data. This is useful, e.g., for
automatically converting human-readable values accepted by the API to
the value the database expects for aChoiceField
.
Used this in a Review Board API resource.
Description | From | Last Updated |
---|---|---|
This should live in djblets/webapi/resources/mixins/forms.py. |
chipx86 | |
Can you change Model to django.db.models.Model? After having played with the doc stuff a bit, I realized we need to … |
chipx86 | |
Blank line between these. |
chipx86 | |
Similar here: django.forms.Form |
chipx86 | |
NotImplementedError isn't really right here. That'd be more for a function that should have been overridden. A better thing to … |
chipx86 | |
Can you also add a module-wide docstring? Even just a summary is fine. That will help it show up in … |
chipx86 | |
I believe this is django.db.models.Model. |
chipx86 | |
"non-None" seems weird. How about "%s must define a form_class attribute." ? |
chipx86 | |
Let's make sure this whole flow takes the request and all the kwargs from the caller and passes all the … |
chipx86 |
- Commit:
-
7f5a3e1022adfe9db63e3a048f6e0ccd8399f0584387409e1d82f8c6d95cca5bb69ae7d905e15587
- Diff:
-
Revision 2 (+83)
-
Tool: PEP8 Style Checker Processed Files: djblets/webapi/mixins.py Tool: Pyflakes Processed Files: djblets/webapi/mixins.py
-
There's a
)
missing in the first paragraph of your change description. -
-
Can you change
Model
todjango.db.models.Model
? After having played with the doc stuff a bit, I realized we need to really be using explicit module paths in these cases, so that linking will work well. -
-
-
NotImplementedError
isn't really right here. That'd be more for a function that should have been overridden. A better thing to do is:assert self.form_class, '%s does not have ....' % ...
- Description:
-
The
UpdateFormMixin
is a mixin for aWebAPIResource
that allows aresource to specify a form class (which should be a subclass of ~ django.forms.ModelForm
to use for creating and updating of model~ django.forms.ModelForm
) to use for creating and updating of modelinstances. This allows the resource that are very basic wrappers around models to have their logic further simplified. This mixin is required for allowing partial updating of models.
Traditionally, a ModelForm
can take aninstance
parameterspecifying an already existing model instance to update, but will still require all required field to specified in the form data. This mixin takes care of that by extracting missing form data from the instance and inserting it into the form data in the proper format (such as setting it to be a list of primary keys in the case of a ModelMultipleChoiceField
).This mixin also allows resources to specify special functions for
parsing values out of the recieved form data. This is useful, e.g., for automatically converting human-readable values accepted by the API to the value the database expects for a ChoiceField
. - Commit:
-
4387409e1d82f8c6d95cca5bb69ae7d905e155871cbef5d99cdef9fa94cca3c5507071b15fe29aa9
-
Tool: Pyflakes Processed Files: djblets/webapi/resources/mixins/forms.py Tool: PEP8 Style Checker Processed Files: djblets/webapi/resources/mixins/forms.py