0

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.

1
  • 1
    Your code doesn't do what you think it does - or rather, it is not fool proof. It is better if you override the clean method. Commented Jul 9, 2018 at 14:46

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.