Hello Guys!
I got this models
class Incarico(models.Model):
polizza = models.OneToOneField(Polizza, default=None, blank=True, on_delete=models.CASCADE, null=True)
...
...
class Polizza(models.Model):
....
....
(many fields)
when i save the form related to “Incarico” model, compiled by some users, i also generate “automatically” the “Polizza” linked to Incarico as an empty editable form.
Polizza will be a complex form so i need to ensure that everything works fine.
When i save the Incarico form (CustomerForm) i use this view.
def home(request):
form = CustomerForm(user=request.user, data=request.POST)
polizza_form = PolizzaForm(data=request.POST)
if request.method == 'POST':
if form.is_valid():
polizza_form.save()
form.polizza = polizza_form
form.save()
return redirect('/incarico_slice')
else:
print(form.errors)
context = {'form':form}
return render(request, 'incarico/incarico.html', context)
And when i want to edit or view the polizza of that Incarico i wrote this dynamic view.
def polizza(request, my_id):
rifer = Incarico.objects.get(id=my_id)
form = PolizzaForm(instance=rifer)
if request.method == 'POST':
form = PolizzaForm(request.POST, instance=rifer)
if form.is_valid():
form.save(commit=True)
return redirect('/incarico_slice')
else:
print(form.errors)
context = {'form':form}
return render(request, 'polizza/polizza.html', context)
For every Incarico, i have a view in html, and dynamically generate the buttons to go to his Polizza like this:
<a href="/polizza/{{ object.id }}"><button>POLIZZA</button></a>
When i go to that link, seems everything works fine, but when i submit the form the database doesn’t save it correctly, the shell tells me i get the POST request, but i can’t see that nowhere in my database, just the entry created at the moment i create the “Incarico”
Every help is appreciated, thanks in advance!!!