Python django.db.utils.IntegrityError:非空约束失败:appname\u modelName.id
我正在django创建一个客户管理系统 我有两种型号,分别是“客户端”和“分期付款”。我的'models.py'文件如下所示: 型号.pyPython django.db.utils.IntegrityError:非空约束失败:appname\u modelName.id,python,python-3.x,django,django-models,Python,Python 3.x,Django,Django Models,我正在django创建一个客户管理系统 我有两种型号,分别是“客户端”和“分期付款”。我的'models.py'文件如下所示: 型号.py from django.db import models from django.utils import timezone from django.urls import reverse # Create your models here. class Client(models.Model): name = models.CharField
from django.db import models
from django.utils import timezone
from django.urls import reverse
# Create your models here.
class Client(models.Model):
name = models.CharField(max_length = 100)
dob = models.SlugField(max_length = 100)
CNIC = models.SlugField(max_length = 100)
property_type = models.CharField(max_length = 100)
down_payment = models.IntegerField()
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('client_details',kwargs={ 'pk' : self.pk})
class Installment(models.Model):
client = models.ForeignKey(Client, blank=True, on_delete=models.CASCADE)
installment_month = models.CharField(max_length = 100)
installment_amount = models.IntegerField()
installment_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.installment_month
def get_absolute_url(self):
return reverse('installment_confirmation')
我在分期付款模型中使用ForeignKey将客户机模型链接到它,因为一个客户机可以有多个分期付款
当我运行以下两个命令时
python manage.py makemigrations
python manage.py migrate
我没有得到任何错误
然后,我使用以下命令运行服务器:
python manage.py runserver
并成功运行
然后,我在我的应用程序中添加了一个使用客户端模型的新客户端
但是,当我想向客户端添加分期付款时,is从用户的字段(分期付款月和分期付款月)中获取输入,当我单击“添加分期付款”时,它会给出以下错误:
IntegrityError at /1/Add-Installment
NOT NULL constraint failed: property_details_installment.client_id
Request Method: POST
Request URL: http://127.0.0.1:8000/1/Add-Installment
Django Version: 2.1.3
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: property_details_installment.client_id
Exception Location: /anaconda3/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 296
Python Executable: /anaconda3/bin/python3
Python Version: 3.6.5
Python Path:
['/Users/razajaved/Documents/installment_plan',
'/anaconda3/lib/python36.zip',
'/anaconda3/lib/python3.6',
'/anaconda3/lib/python3.6/lib-dynload',
'/anaconda3/lib/python3.6/site-packages',
'/anaconda3/lib/python3.6/site-packages/aeosa']
Server time: Sun, 28 Mar 2021 13:55:36 +0000
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-abd51507c2e0> in <module>()
----> 1 client1.id
AttributeError: 'QuerySet' object has no attribute 'id'
我接下来尝试的是从分期付款模型的ForeignKey行中删除“blank=True”参数。然后它成功地添加分期付款
但是,要检查添加的分期付款是否与添加的客户端正确对应。我使用以下命令进入shell:
python manage.py shell
client1 = Client.objects.filter('clientName')
在shell中,我执行了以下操作:
from property_details.models import Client
from property_details.models import Installment
然后,我尝试使用以下命令检查我添加的客户端:
python manage.py shell
client1 = Client.objects.filter('clientName')
它起作用了
我还可以使用上面的命令检查添加的分期付款
但是当我跑的时候
client1.id
installment.id
它给出了以下错误:
IntegrityError at /1/Add-Installment
NOT NULL constraint failed: property_details_installment.client_id
Request Method: POST
Request URL: http://127.0.0.1:8000/1/Add-Installment
Django Version: 2.1.3
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: property_details_installment.client_id
Exception Location: /anaconda3/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 296
Python Executable: /anaconda3/bin/python3
Python Version: 3.6.5
Python Path:
['/Users/razajaved/Documents/installment_plan',
'/anaconda3/lib/python36.zip',
'/anaconda3/lib/python3.6',
'/anaconda3/lib/python3.6/lib-dynload',
'/anaconda3/lib/python3.6/site-packages',
'/anaconda3/lib/python3.6/site-packages/aeosa']
Server time: Sun, 28 Mar 2021 13:55:36 +0000
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-abd51507c2e0> in <module>()
----> 1 client1.id
AttributeError: 'QuerySet' object has no attribute 'id'
它返回所添加分期付款的id
看起来,我在分期付款模型中使用的ForeignKey存在一些问题,并且没有为客户机分配id。我尝试过不同的解决方案,比如输入“blank=True”或“null=True”参数。但这对我不起作用
有人知道如何解决这个问题吗?我看到的一个错误是:
client1=Client.objects.filter('clientName')
应该是这样的:
client1=Client.objects.filter('clientName').first()
filter()
返回QuerySet
您必须通过first()
或切片获取模型实例,然后只有您才能访问client1.id等字段