PatchModelForm - A ModelForm subclass with the semantics of the PATCH HTTP method

 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

  1. Username form field by sma 5 years, 5 months ago
  2. duplicate model object merging script by nstrite 6 years, 7 months ago
  3. Prefill ForeignKey caches by insin 5 years, 6 months ago
  4. Modifying the fields of a third/existing model class by marinho 3 years, 3 months ago
  5. LoginAsForm - Login as any User without a password by johnboxall 4 years, 10 months ago

Comments

umbrae (on May 28, 2012):

On line 12 you have UpdateModelForm - I assume that's meant to be PatchModelForm.

#

(Forgotten your password?)