class requistiondetail(models.Model):
requistionid=models.ForeignKey(requistion,on_delete=models.CASCADE)
emp_name=models.ForeignKey(employee,on_delete=models.CASCADE)
detail_type=models.ForeignKey(requistiondetailtype,on_delete=models.CASCADE)
period_claimed=models.ForeignKey(period,on_delete=models.CASCADE)
ndays=models.IntegerField(default=0,verbose_name="Number of days")
startdate=models.DateField(null=True)
enddate=models.DateField(null=True)
rate=models.FloatField(default=0.0,verbose_name='Allowances Rate')
amount=models.FloatField(default=0.0,verbose_name='Amount')
objects = models.Manager()
class Meta:
verbose_name_plural='requistiondetails'
I can add the parent record successfully and have them listed in a table as per image below.

I want to add the details by clicking on the add details button.
Here is the view code for adding the details
def add_details(request,requistion_id):
myrequistion=requistion.objects.get(id=requistion_id)
if request.method !='POST':
# if no data submitted ,create a blank form
form=RequistiondetailForm()
else:
# post data submitted
form=RequistiondetailForm(data=request.POST)
if form.is_valid():
details=form.save(commit=False)
#print(myrequistion.pk)
details.requistion=myrequistion
print(type(myrequistion))
details.save()
return redirect('requistions:requistion_get',pk=requistion_id)
#display a blank or invalid form
context={'requistion':myrequistion,'form': form}
return render(request,'requistions/requistion_detail_add.html',context)
Here is the html form for rendering the detail record and it is done perfectly.
{% extends 'requistions/base.html'%}
{% block content %}
<p>
<a href="{% url 'requistions:requistion_get' requistion.pk %}"> {{requistion.purpose}}</a>
</p>
<p> Add Requistion Details </p>
<form action="{% url 'requistions:add_details' requistion.pk %}" method="POST">
{% csrf_token %}
{{ form.as_p}}
<button name="submit"> Add Line </button>
</form>
{% endblock content %}

Problem is when, i click submit button, i get the error below:
NOT NULL constraint failed: requistions_requistiondetail.requistionid_id
I have added the print statement in the add_details view and the requestid parameter is properly passed to the method.
Why is it not saved to the model?
Any thing i am not doing right?