With the help of the Form.changed_data list, the Form.initial dict, and the Form.cleaned_data dict, Django makes it easy to compare changes in a form.
For example, we can override the form_valid method in an UpdateView to print the old and new values of all the changed fields as below:
def form_valid(self, form):
for fieldname in form.changed_data:
print(fieldname)
print("old=%s" % form.initial[fieldname])
print("new=%s" % form.cleaned_data[fieldname])
return super().form_valid(form)
References:
Form.changed_data
Form.initial
Form.cleaned_data
No comments:
Post a Comment