我看到了有关此错误的一些主题,但是找不到答案。
这是我的错误:
django.db.utils.IntegrityError: NOT NULL constraint failed: domain_analyse_practice.header_id
我不太明白为什么会发生此错误。
我的模型。py:
class Header(models.Model):
name = models.CharField(max_length=40, null=False)
description = models.CharField(max_length=500, null=False)
class Practice(models.Model):
nature = models.CharField(max_length=10, null=False)
value = models.CharField(max_length=300, null=False)
further = models.CharField(max_length=50, null=True, blank=True)
header = models.ForeignKey(Header, on_delete=models.CASCADE)
def value_cleaner(self):
self.value = self.value.replace("‘","'").replace("’", "'")
def __init__(self, *args, **kwargs):
super(Practice, self).__init__()
self.value_cleaner()
这是我的init_db.py脚本,它负责填充数据库:
def init_practice_db(filename):
with open('config/' + filename, newline='') as csv_file:
reader = csv.reader(csv_file, delimiter=',', quotechar='|')
i = 0
for row in reader:
if i != 0:
# Not the header row
header_name = row[0]
# Select the header in database with header name
header = Header.objects.get(name=header_name)
further = None
nature = row[1].rstrip()
value = row[2].rstrip()
if row[3] != "None":
further = further
Practice.objects.create(nature=nature, value=value, further=further, header=header)
i += 1
如果缺少某些信息,请告诉我:) 谢谢您的回答。
答案 0 :(得分:3)
您没有将任何参数传递给super(Practice, self).__init__()
。试试这个
class Practice(models.Model):
...
def __init__(self, *args, **kwargs):
super(Practice, self).__init__(*args, **kwargs)
self.value_cleaner()