I see some topics about this error but I don't find my answer in.
here is my error:
django.db.utils.IntegrityError: NOT NULL constraint failed: domain_analyse_practice.header_id
I don't really understand why this error happens.
My models.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()
And here is my init_db.py script which is in charge of populate the database:
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
If some information are missing let me know :) Thanks for you answers.
You are not passing any arguments to super(Practice, self).__init__()
. Try this
class Practice(models.Model):
...
def __init__(self, *args, **kwargs):
super(Practice, self).__init__(*args, **kwargs)
self.value_cleaner()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.