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.)
-
-
-
-
-
-
-
-
-
reviewboard/webapi/mixins.py (Diff revision 1) What does this do? Just provide a default? What happens if the form doesn't support
extra_data
? -
reviewboard/webapi/mixins.py (Diff revision 1) 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 list of form fields, excluded fields, and returns a dictionary that can be passed in to a form's
initial=
. Sounds like this is what we want? -
-
Change Summary:
Now refactored into Djblets.
Description: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Depends On: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Commit: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Diff: |
Revision 2 (+48) |
-
Tool: PEP8 Style Checker Processed Files: reviewboard/webapi/mixins.py Tool: Pyflakes Processed Files: reviewboard/webapi/mixins.py
Commit: |
|
||||
---|---|---|---|---|---|
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: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Commit: |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Diff: |
Revision 4 (+51 -1) |
-
Tool: Pyflakes Processed Files: reviewboard/webapi/mixins.py Tool: PEP8 Style Checker Processed Files: reviewboard/webapi/mixins.py
-
-
reviewboard/webapi/mixins.py (Diff revision 4) In this case, it'd be better to do:
from djblets.webapi.resources.mixins.forms import ( UpdateFormMixin as DjbletsUpdateFormMixin)
That leaves room for additional imports to take place on following lines.
-