1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | from django import forms
from django.core.exceptions import ValidationError
from django.forms.fields import FileField
class PatchModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
for fieldname in self.base_fields:
self.base_fields[fieldname].required = False
self.empty_permited = True
return super(UpdateModelForm, self).__init__(*args, **kwargs)
def _clean_fields(self):
for name, field in self.fields.items():
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
value = field.widget.value_from_datadict(
self.data,
self.files,
self.add_prefix(name)
)
# this is the key difference with the inherited behavior so fields
# not present in the bound data do not get updated with None values
if self.add_prefix(name) not in self.data:
continue
try:
if isinstance(field, FileField):
initial = self.initial.get(name, field.initial)
value = field.clean(value, initial)
else:
value = field.clean(value)
self.cleaned_data[name] = value
if hasattr(self, 'clean_%s' % name):
value = getattr(self, 'clean_%s' % name)()
self.cleaned_data[name] = value
except ValidationError, e:
self._errors[name] = self.error_class(e.messages)
if name in self.cleaned_data:
del self.cleaned_data[name]
|
More like this
- LoginAsForm - Login as any User without a password by johnboxall 3 years, 11 months ago
- Modifying the fields of a third/existing model class by marinho 2 years, 4 months ago
- Clean Reversion History: Remove unimportant Changes by guettli 1 year, 3 months ago
- Username form field by sma 4 years, 6 months ago
- Prefill ForeignKey caches by insin 4 years, 7 months ago
Comments
On line 12 you have
UpdateModelForm- I assume that's meant to bePatchModelForm.#