Add a mixin for updating models with a ModelForm
Review Request #7493 — Created July 5, 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
.This is a re-export of the mixin from Djblets with some extra
functionality added.
Used the mixin in a model. It worked as expected.
Description | From | Last Updated |
---|---|---|
Should use :py:attr: to reference form_class. |
chipx86 | |
:py:class: for ModelForm. |
chipx86 | |
:py:class: here too. |
chipx86 | |
:py:meth: for these. |
chipx86 | |
:py:class: and :py:attr: |
chipx86 | |
Blank line between these. |
chipx86 | |
:py:class:, and trailing period. |
chipx86 | |
What does this do? Just provide a default? What happens if the form doesn't support extra_data? |
chipx86 | |
So Django's form support actually has a nifty function we probably want to use: django.forms.models.model_to_dict. This takes an instance, a … |
chipx86 | |
Blank line between these. |
chipx86 | |
Should have a trailing period. |
chipx86 | |
In this case, it'd be better to do: from djblets.webapi.resources.mixins.forms import ( UpdateFormMixin as DjbletsUpdateFormMixin) That leaves room for additional … |
chipx86 | |
django.db.models.Model |
chipx86 |
-
How about moving this over to Djblets? This would be useful outside of Review Board (and I'm working to take all the neat "core" stuff and make it part of Djblets anyway.)
- Change Summary:
-
Now refactored into Djblets.
- 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 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
.+ + This is a re-export of the mixin from Djblets with some extra
+ functionality added. - Depends On:
-
- Commit:
ec0ab8fb7aa8499dc0fba35894196dee80e7359c- Diff:
Revision 2 (+48)
-
Tool: PEP8 Style Checker Processed Files: reviewboard/webapi/mixins.py Tool: Pyflakes Processed Files: reviewboard/webapi/mixins.py
- Commit:
-
ec0ab8fb7aa8499dc0fba35894196dee80e7359c9d72aac2831365eb7ca27f5b61dfaaf8c89118ba
- Diff:
-
Revision 3 (+50)
-
Tool: PEP8 Style Checker Processed Files: reviewboard/webapi/mixins.py Tool: Pyflakes Processed Files: reviewboard/webapi/mixins.py
- Change Summary:
-
Rebase off updated Djblets changes.
- 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
.This is a re-export of the mixin from Djblets with some extra
functionality added. - Commit:
-
9d72aac2831365eb7ca27f5b61dfaaf8c89118ba5ace99904af65e1f9b4f1dee54ca1c29e47b5eb3
- Diff:
-
Revision 4 (+51 -1)
-
Tool: Pyflakes Processed Files: reviewboard/webapi/mixins.py Tool: PEP8 Style Checker Processed Files: reviewboard/webapi/mixins.py