you need to pass the instance of the requisition id to your form before you
can save it.
You can try this:
             myrequistion=requistion.objects.get(id=requistion_id)
        form=RequistiondetailForm(request.POST, inctance= myrequistion)
        if form.is_valid()
            form.save()

On Fri, Jul 10, 2020 at 3:14 PM Ashutosh Mishra <ashutoshmishra...@gmail.com>
wrote:

> That is a data base related error,this happens when you have already
> inserted data to a field of a model then try to manipulate that model
> again.If you know how to handle database then delete that or,delete this
> whole database and start again.
>
>
> On Thursday, July 9, 2020 at 3:49:21 PM UTC+5:30 ronald.kamu...@gmail.com
> wrote:
>
>> Greetings members.
>>
>> I am a learner and trying to create an app where staff members can submit
>> online requests for field allowances.
>>
>> Here my major models.
>> class requistion(models.Model):
>>     made_by=models.ForeignKey(employee,on_delete=models.CASCADE)
>>     date_made=models.DateTimeField(auto_now_add=True)
>>     purpose=models.TextField(verbose_name='Purpose')
>>     objects = models.Manager()
>>     class Meta:
>>         verbose_name_plural='requistion'
>>     def __str__ (self):
>>         return self.purpose
>> 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.
>>
>> [image: list.png]
>>
>> 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 %}
>>
>> [image: form.png]
>>
>> 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?
>>
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/bd5cf1b1-4b86-46d1-af7e-2f493818a59an%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/bd5cf1b1-4b86-46d1-af7e-2f493818a59an%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPCf-y6NRoTqDSewBrBgycg2sVVwecs0-hF0mqzH2eD16uooPQ%40mail.gmail.com.

Reply via email to