[英]how to populate django model with a foreign key from a csv
my model.py: 我的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)
文件“ C:\\ Users \\ Wynt \\ AppData \\ Local \\ conda \\ conda \\ envs \\ DjangoEnv \\ lib \\ site-packages \\ django \\ db \\ backends \\ utils.py”,行85,在_execute返回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文件“ C:\\ Users \\ Wynt \\ AppData \\ Local \\ conda \\ conda \\ envs \\ DjangoEnv \\ lib \\ site-packages \\ django \\ db \\ utils.py”,第89行,位于__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)文件“ C:\\ Users \\ Wynt \\ AppData \\ Local \\ conda \\ conda \\ envs \\ DjangoEnv \\ lib \\ site-packages \\ django \\ db \\ backends \\ utils.py”,行_execute中的返回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在执行返回Database.Cursor的行中,文件“ C:\\ Users \\ Wynt \\ AppData \\ Local \\ conda \\ conda \\ envs \\ DjangoEnv \\ lib \\ site-packages \\ django \\ db \\ backends \\ sqlite3 \\ base.py”。 execute(self,query,params)django.db.utils.IntegrityError:NOT NULL约束失败:basic_app_teams.category_id
Make Sure while creating the Teams object the category is never empty ie value coming from csv at row[4]. 确保在创建Teams对象时类别永远不会为空,即,值来自csv的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.
换句话说,如果row [4]在任何时间点都为空,则将引发异常,因为在Model类别中是强制性外键。 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 或确保在创建Teams对象时类别的值永远不会为空
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.