Answer the question
In order to leave comments, you need to log in
How to update only changed fields in django?
There is a form, the fields are optional, upon receipt on the server, I check whether there is a record with such an id or not, if it exists, I pass initial and the current form, then I check change_data and supplement the existing model using them (if the fields != '' or None) .
try:
current_user = Passport.objects.get(id=current_id)
f = PassportForm(request.POST, request.FILES, initial=model_to_dict(current_user))
if f.has_changed():
for i in f.changed_data:
if (f.data[i] != '') and (f.data[i] != None):
current_user.i = f.data[i]
current_user.__setattr__(i, f.data[i])
current_user.save()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question