Instance partial update

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
Add this method to a custom form to use as an alternative
to the save method.  When passed an object instance, the it
will set values on the model corresponding to matching
field names on the form.  

Example:

myobject = MyObject.objects.get(pk=1) 
form = MyForm(request.POST) 
    if form.is_valid(): 
        form.update_instance(myobject)
"""
def update_instance(self,instance,commit=True):
    for f in instance._meta.fields:
        if f.attname in self.fields:
            setattr(instance,f.attname,self.cleaned_data[f.attname])
    if commit:
        try: 
            instance.save()
        except: 
            return False
    return instance

More like this

  1. Complex Formsets by smagala 4 years, 4 months ago
  2. BigIntegerField and BigAutoField by fnl 4 years, 5 months ago
  3. SortableModel - abstract model class for sortable records by bendavis78 3 years, 11 months ago
  4. partial tag by bl4th3rsk1t3 3 years, 11 months ago
  5. PatchModelForm - A ModelForm subclass with the semantics of the PATCH HTTP method by gnrfan 11 months, 4 weeks ago

Comments

(Forgotten your password?)