Я создаю довольно простое приложение в Django, где есть 3 модели. Пациенты, Отец и Мать. Я назначаю иностранный ключ от отца-матери в модели Паитент. Намерение - получить подробности о пациенте, включая данные родителя, и сохранить его в базе данных, но я получаю IntegrityError в / NOT NULL ограничение не выполнено: myapp_paitents.father_id Я пытался выполнить поиск по той же проблеме, но у меня его не было. Пожалуйста, кто-нибудь, помогите мне с этим.
файл models.py
from django.db import models
class Father(models.Model):
f_name = models.CharField(null=True, max_length=20)
f_age = models.IntegerField(null=True)
f_occupation = models.CharField(null=True, max_length=20)
f_edu = models.CharField(null=True, max_length=20)
f_income = models.FloatField(null=True)
def __str__(self):
return self.f_name
class Mother(models.Model):
m_name = models.CharField(null=True, max_length=20)
m_age = models.IntegerField(null=True)
m_occupation = models.CharField(null=True, max_length=20)
m_edu = models.CharField(null=True, max_length=20)
m_income = models.FloatField(null=True)
def __str__(self):
return self.m_name
class Paitents(models.Model):
p_name = models.CharField(null=True, max_length=20)
p_age = models.IntegerField(null=True)
p_edu = models.CharField(null=True, max_length=20)
p_add_com = models.CharField(null=True, max_length=200)
p_add = models.CharField(null=True, max_length=200)
father = models.ForeignKey(Father, on_delete=models.CASCADE)
mother = models.ForeignKey(Mother, on_delete=models.CASCADE)
def __str__(self):
return self.p_name
views.py
from django.shortcuts import render
from .models import *
def index(request):
if request.method == 'POST':
paitent = Paitents()
fathers = Father()
mothers = Mother()
fathers.f_name = request.POST.get('f_name')
fathers.f_age = request.POST.get('f_age')
fathers.f_occupation = request.POST.get('f_occupation')
fathers.f_edu = request.POST.get('f_edu')
fathers.f_income = request.POST.get('f_income')
mothers.m_name = request.POST.get('m_name')
mothers.m_age = request.POST.get('m_age')
mothers.m_occupation = request.POST.get('m_occupation')
mothers.m_edu = request.POST.get('m_edu')
mothers.m_income = request.POST.get('m_income')
paitent.p_name = request.POST.get('p_name')
paitent.p_age = request.POST.get('p_age')
paitent.p_edu = request.POST.get('p_edu')
paitent.p_add = request.POST.get('p_add')
paitent.p_add_com = request.POST.get('p_add_com')
fathers.save()
mothers.save()
paitent.save()
return render(request,'paitent_d/home.html')
else:
return render(request,'paitent_d/home.html')
Html page
<form action="" method="POST">
{%csrf_token%}
<h1>Paitent Details</h1>
<hr>
<!--<input type="text" id="p_ID" name="p_ID" placeholder="ID"><br>-->
<input type="text" id="p_name" name="p_name" placeholder="Name"><br>
<input type="text" id="p_age" name="p_age" placeholder="Age"><br>
<input type="text" id="p_edu" name="p_edu" placeholder="Education"><br>
<input type="text" id="p_add_com" name="p_add_com" placeholder="Communication Address"><br>
<input type="text" id="p_add" name="p_add" placeholder="Address"><br>
<hr>
<input type="text" id="f_name" name="f_name" placeholder="Father's Name"><br>
<input type="text" id="f_age" name="f_age" placeholder="Age"><br>
<input type="text" id="f_occupation" name="f_occupation" placeholder="Occupation"><br>
<input type="text" id="f_edu" name="f_edu" placeholder="Education"><br>
<input type="text" id="f_income" name="f_income" placeholder="Income"><br>
<hr>
<input type="text" id="m_name" name="m_name" placeholder="Mother's Name"><br>
<input type="text" id="m_age" name="m_age" placeholder="Age"><br>
<input type="text" id="m_occupation" name="m_occupation" placeholder="Occupation"><br>
<input type="text" id="m_edu" name="m_edu" placeholder="Education"><br>
<input type="text" id="m_income" name="m_income" placeholder="Income"><br><br>
<input type="submit" value="Submit"><br>
</form>
Ошибка, которую я получил
File "/home/rahul/dev/health/myenv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: myapp_paitents.father_id
