[英]NOT NULL constraint failed for ForeignKey(null=True)
I have two models, namely Applicant
and LoanRequest
.我有两个模型,即Applicant
和LoanRequest
。 Whenever an Applicant
instance is created, a signal is sent to a function that makes an API call.每当创建Applicant
实例时,都会向进行 API 调用的函数发送信号。 The data from the call, along with the primary key of the instance that sent the signal, is saved to as a LoanRequest
.来自调用的数据以及发送信号的实例的主键被保存为LoanRequest
。
When I save the LoanRequest
, however, it gives the following error:但是,当我保存LoanRequest
时,它会出现以下错误:
django.db.utils.IntegrityError: NOT NULL constraint failed: queues_loanrequest.dealer_id_id
Here's my code:这是我的代码:
class Applicant(models.Model):
app_id = models.CharField(max_length=100)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.first_name + " " + self.last_name
class LoanRequest(models.Model):
loan_request_id = models.CharField(max_length=100)
app_id = models.ForeignKey(Applicant, on_delete=models.CASCADE)
FICO_score = models.CharField(max_length=100)
income_risk_score = models.CharField(max_length=100)
DTI = models.CharField(max_length=100)
date_requested = models.DateTimeField(default=timezone.now)
loan_request_status = models.CharField(max_length=100,blank=True)
dealer_id = models.ForeignKey(Dealer, on_delete=models.CASCADE,blank=True)
def credit_check(sender, instance, **kwargs):
credit_json = get_credit(instance.pk) #credit information for applicant
credit_json = json.loads(credit_json)
new_request = LoanRequest.objects.create(app_id=instance, FICO_score=credit_json["ficco-score"],
income_risk_score=credit_json["income-risk-score"],
DTI=credit_json["DTI"])
Very new to Django, would greatly appreciate help!对 Django 非常陌生,非常感谢帮助!
LoanRequest
has a field LoanRequest
有一个字段
dealer_id = models.ForeignKey(Dealer, on_delete=models.CASCADE,blank=True)
Because dealer_id
doesn't have null=True
, creating a LoanRequest
instance without supplying a dealer_id
will raise the database error you've got.由于dealer_id
没有null=True
,因此在不提供dealer_id
情况下创建LoanRequest
实例将引发您遇到的数据库错误。
You need to either supply a dealer_id
in the instantiation of the LoanRequest
, which should be a Dealer
instance, or else change your LoanRequest
model so that the dealer_id
field has null=True
.您需要在LoanRequest
的实例化中提供一个dealer_id
,它应该是一个Dealer
实例,或者更改您的LoanRequest
模型,以便dealer_id
字段为null=True
。
Please see comment above re: using the suffix _id
on a Django model foreign key - it's nearly always incorrect to do so.请参阅上面的评论:在 Django 模型外键上使用后缀_id
- 这样做几乎总是不正确的。
Specifying blank=True
without null=True
is not very useful for a ForeignKey.在没有null=True
blank=True
情况下指定blank=True
对 ForeignKey 不是很有用。
Use this instead:改用这个:
dealer_id = models.ForeignKey(Dealer, on_delete=models.CASCADE, blank=True, null=True)
This would make the field optional.这将使该字段可选。 Otherwise, you would have to specify the Dealer
every time you create a LoanRequest
.否则,每次创建LoanRequest
时都必须指定Dealer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.