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. |
|
|
Can you change Model to django.db.models.Model? After having played with the doc stuff a bit, I realized we need to … |
|
|
Blank line between these. |
|
|
Similar here: django.forms.Form |
|
|
NotImplementedError isn't really right here. That'd be more for a function that should have been overridden. A better thing to … |
|
|
Can you also add a module-wide docstring? Even just a summary is fine. That will help it show up in … |
|
|
I believe this is django.db.models.Model. |
|
|
"non-None" seems weird. How about "%s must define a form_class attribute." ? |
|
|
Let's make sure this whole flow takes the request and all the kwargs from the caller and passes all the … |
|
Commit: |
|
||||
---|---|---|---|---|---|
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. -
djblets/webapi/mixins.py (Diff revision 2) This should live in
djblets/webapi/resources/mixins/forms.py
. -
djblets/webapi/mixins.py (Diff revision 2) 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. -
-
-
djblets/webapi/mixins.py (Diff revision 2) 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: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Commit: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Diff: |
Revision 3 (+84) |

-
Tool: Pyflakes Processed Files: djblets/webapi/resources/mixins/forms.py Tool: PEP8 Style Checker Processed Files: djblets/webapi/resources/mixins/forms.py
-
-
djblets/webapi/resources/mixins/forms.py (Diff revision 3) Can you also add a module-wide docstring? Even just a summary is fine. That will help it show up in the docs. (Blank line after the docstring.)
Speaking of which, can you add this to
docs/djblets/coderef/index.rst
? -
djblets/webapi/resources/mixins/forms.py (Diff revision 3) I believe this is
django.db.models.Model
. -
djblets/webapi/resources/mixins/forms.py (Diff revision 3) "non-None" seems weird. How about "%s must define a form_class attribute." ?
-
-
djblets/webapi/resources/mixins/forms.py (Diff revision 3) Let's make sure this whole flow takes the request and all the kwargs from the caller and passes all the way through the parse function. We used to not do this for field serialization, and regretted it. Sooner or later, something will need that data.