0
votes

my model.py:

# Riders models ----------------------------------------
class CategoryTeam(models.Model):

    name = models.CharField(max_length = 256)

    def __str__(self):
        return self.name

class Teams(models.Model):

    name = models.CharField(max_length = 256)
    code = models.CharField(max_length = 10)
    nation = models.CharField(max_length = 10)
    continent = models.CharField(max_length = 10)
    category = models.ForeignKey(CategoryTeam, on_delete=models.CASCADE,)

    def __str__(self):
        return self.name

#-----------------------------------------------------#

my script to populate

from basic_app.models import CategoryTeam,Teams



def populateCat():
    f = open('CategoryCSV.csv')
    reader = csv.reader(f)
    next(reader)
    for row in reader:

        # Create new User Entry
        category = CategoryTeam.objects.get_or_create(name=row[0])[0]

def populateTeamat():
    f = open('FantaDS Project - Teams.csv')
    reader = csv.reader(f)
    next(reader)
    for row in reader:

        # Create new User Entry
        team = Teams.objects.get_or_create(
                                        name = row[0],
                                        code = row[1],
                                        nation = row[2],
                                        continent = row[3],
                                        category = row[4]
                                        )[0]

if __name__ == '__main__':
    print("Populating the databases Cat...Please Wait")
    populateCat()
    print('Populating Complete')
    print("Populating the databases Team...Please Wait")
    populateTeamat()
    print('Populating Complete')

the exeption generated:

File "C:\Users\Wynt\AppData\Local\conda\conda\envs\DjangoEnv\lib\site-packages\django\db\backends\utils.py",line 85, in _execute return self.cursor.execute(sql, params)
File "C:\Users\Wynt\AppData\Local\conda\conda\envs\DjangoEnv\lib\site-packages\django\db\utils.py", line 89, in __exit__raise ndj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Wynt\AppData\Local\conda\conda\envs\DjangoEnv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params)
File "C:\Users\Wynt\AppData\Local\conda\conda\envs\DjangoEnv\lib\site-packages\django\db\backends\sqlite3\base.py",line 303, in execute return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: NOT NULL constraint failed: basic_app_teams.category_id

1
You've commented out the line that sets the category, but that is a required field. Why did you do that?Daniel Roseman
I’m trying to solve it and i’d copied bere the wrong version,sorrymarcantonio sofia

1 Answers

0
votes
  1. Make Sure while creating the Teams object the category is never empty i.e value coming from csv at row[4]. In other words if row[4] will be empty at any point in time it will throw the exception because in the Model category is a compulsory foreign key. either make it

    category = models.ForeignKey(CategoryTeam, on_delete=models.CASCADE, null=True, black=True)
    or make sure the category's value is never empty while creating Teams object