Answer the question
In order to leave comments, you need to log in
What is the best way to track model field changes through a form?
Hello. There is a user model MyUser. In the form UserInf I change I give the user the opportunity to change from 0 to all fields of the form and, accordingly, the model. How to keep track of which fields are changed and not overwrite unmodified fields with default empty strings?
At the moment there is the following view:
def profile_change(request):
if request.method == 'GET':
current_inf = MyUser.objects.get(id = request.user.id)
form = UserInf()
return render(request, 'profile_change.html', context={'edit_form':form,
'user':current_inf})
elif request.method == 'POST':
form = UserInf(request.POST)
if form.is_valid():
current_password = request.user.password
form_cleaned = form.cleaned_data
if check_password(form_cleaned['old_password'],current_password):
if form_cleaned['password'] == form_cleaned['password2']:
user = form.save(commit=False)
user.password = form_cleaned['password']
user.id = request.user.id
user.user = request.user
user.save()
id = request.user.id
else:
pass
else:
pass
user = MyUser.objects.get(id=request.user.id)
# как отследить, какие поля формы заполнены?
return HttpResponseRedirect(reverse('profile', kwargs={'id':id}))
else:
pass
class UserInf(forms.ModelForm):
old_password = forms.CharField(widget=forms.PasswordInput, label="Старый пароль", required=False)
password = forms.CharField(widget=forms.PasswordInput, label="Введите пароль", required=False)
password2 = forms.CharField(widget=forms.PasswordInput, label="Подтвердите пароль",required=False)
class Meta:
model = MyUser
fields = ('photo','firstname','surname','patronym','country')
labels = {'firstname':'Имя','surname':'Фамилия',
'patronym':'Отчество','country':'Страна'}
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