我有两个模型,即Applicant
和LoanRequest
。每当创建Applicant
实例时,就会向进行API调用的函数发送一个信号。来自调用的数据以及发送信号的实例的主键被保存为LoanRequest
但是,当我保存LoanRequest
时,会出现以下错误:
django.db.utils.IntegrityError: NOT NULL constraint failed: queues_loanrequest.dealer_id_id
这是我的密码:
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"])
非常新的Django,将非常感谢帮助
LoanRequest
有一个字段由于
dealer_id
没有null=True
,因此在不提供dealer_id
的情况下创建LoanRequest
实例将引发数据库错误您需要在
LoanRequest
的实例化中提供一个dealer_id
,它应该是一个Dealer
实例,或者更改LoanRequest
模型,以便dealer_id
字段具有null=True
请参阅上面的评论:在Django模型外键上使用后缀
_id
,这样做几乎总是不正确的指定
blank=True
而不指定null=True
对于ForeignKey不是很有用改用这个:
这将使该字段成为可选字段。否则,每次创建
LoanRequest
时都必须指定Dealer
相关问题 更多 >
编程相关推荐