Answer the question
In order to leave comments, you need to log in
How to add additional data to a form before saving it in Django?
Good day, there is a need to make an additional. data in the form before saving it, I try to do so, but it does not work out.
view
@csrf_protect
def createNewPersonal(request):
form = PersonalForm(request.POST)
if request.method == 'POST':
if form.is_valid():
form.pers_unit = 1
form.save()
return HttpResponseRedirect('/personal/')
return render(request, 'newPersonakForm.html', {'form': form})
class PersonalForm(ModelForm):
class Meta:
model = Personal
fields = ['pers_name', 'pers_passport_data', 'pers_post', 'pers_experience', 'pers_salary', 'pers_military_duty', 'pers_family', 'pers_phone_number', 'pers_unit', 'pers_status']
@csrf_protect
def createNewPersonal(request):
form = PersonalForm(request.POST)
if request.method == 'POST':
form.pers_unit = 1
if form.is_valid():
form.save()
return HttpResponseRedirect('/personal/')
return render(request, 'newPersonakForm.html', {'form': form})
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