之前有效,我在运行导入后收到了这个。 你知道我为什么这个吗?我应该如何改变我的模型。我记得Django正在做authomaticaly后端的外键。你能告诉我这样做有什么好处吗?数据库存在,模型存在,我也进行了迁移和迁移。
这些是我的模特:
from __future__ import unicode_literals
from django.db import models
# from django.core.urlresolvers import reverse
from django.urls import reverse
# Create your models here.
class Ofac_Sdn(models.Model):
number = models.IntegerField(blank=True, null=True)
name = models.CharField(max_length=200, null=True)
b_i = models.CharField(max_length=250, null=True)
programe= models.CharField(max_length=250, null=True)
more_info = models.CharField(max_length=250, null=True)
vessel_call_sign = models.CharField(max_length=250, null=True)
vessel_type= models.CharField(max_length=250, null=True)
vessel_dwt = models.IntegerField(blank=True, null=True)
# vessel_dwt = models.CharField(max_length=250,blank=True, null=True)
tonnage = models.IntegerField(blank=True, null=True)
vessel_flag = models.CharField(max_length=250, null=True)
vessel_owner= models.CharField(max_length=250, null=True)
dob_aka= models.CharField(max_length=250, null=True)
date_of_creation = models.DateTimeField(max_length=250, auto_now=True,blank=True, null=True)
publish_date = models.DateTimeField(max_length=250, auto_now_add=True,blank=True, null=True)
#content = models.TextField(blank=True, null=True, default="")
class Meta:
verbose_name_plural = "ofac_sdn"
def __str__(self):
return ((self.number, self.name, self.programe))
class Ofac_Add(models.Model):
number = models.ForeignKey(Ofac_Sdn, on_delete=models.CASCADE)
n= models.IntegerField(blank=True, null=True)
adresa = models.CharField(max_length=250, null=True)
oras_zip = models.CharField(max_length=250, null=True)
stat = models.CharField(max_length=250, null=True)
ceva = models.CharField(max_length=250, null=True)
date_of_creation = models.DateTimeField(max_length=250, auto_now=True,blank=True, null=True)
publish_date = models.DateTimeField(max_length=250, auto_now_add=True,blank=True, null=True)
class Meta:
verbose_name_plural = "ofac_add"
def __str__(self):
return ((self.number_id, self.adresa , self.oras_zip, self.stat, self.ceva))
class Ofac_Alt(models.Model):
number = models.ForeignKey(Ofac_Sdn, on_delete=models.CASCADE)
ceva = models.CharField(max_length=500, null=True)
aka = models.CharField(max_length=500, null=True)
name_of_aka = models.CharField(max_length=500, null=True)
a = models.CharField(max_length=250, null=True)
date_of_creation = models.DateTimeField(max_length=250, auto_now=True,blank=True, null=True)
publish_date = models.DateTimeField(max_length=250, auto_now_add=True,blank=True, null=True)
class Meta:
verbose_name_plural = "ofac_alt"
def __str__(self):
return ((self.number_id, self.aka, self.name_of_aka))
class Ofac_Sdn_Comments(models.Model):
number = models.ForeignKey(Ofac_Sdn, on_delete=models.CASCADE)
more_info = models.CharField(max_length=1000, null=True)
date_of_creation = models.DateTimeField(max_length=250, auto_now=True,blank=True, null=True)
publish_date = models.DateTimeField(max_length=250, auto_now_add=True,blank=True, null=True)
class Meta:
verbose_name_plural = "ofac_sdn_comments"
def __str__(self):
return ((self.number_id, self.more_info))
导入代码:
import csv, sys, os
# project_dir = "/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/import_save/"
# project_dir = "/Users/Dropbox/ofac_project/ofac_sdn/import_save/" project_dir = "/Users/Dropbox/ofac_project" sys.path.append(project_dir) os.environ['DJANGO_SETTINGS_MODULE']='ofac.settings'
import django django.setup()
from ofac_sdn.models import Ofac_Sdn from ofac_sdn.models import Ofac_Add from ofac_sdn.models import Ofac_Alt from ofac_sdn.models import Ofac_Sdn_Comments
file = 'new_sdn.csv' file1 = 'new_add.csv' file2 ='new_alt.csv' file3 ='new_sdn_comments.csv'
data = csv.reader(open(file),delimiter=",") for row in data:
if row[0] !="Number":
post = Ofac_Sdn()
post.number = row[0]
post.name = row[1]
post.b_i=row[2]
post.programe=row[3]
post.more_info=row[4]
post.vessel_call_sign=row[5]
post.vessel_type=row[6]
post.vessel_dwt=row[7]
# post.vessel_dwt=int(''.join(row[7].split(',')))
post.tonnage=row[8]
post.vessel_flag=row[9]
post.vessel_owner=row[10]
post.dob_aka=row[11]
post.save()
data1 = csv.reader(open(file1),delimiter=",") for row in data1:
if row[0] !="Number":
post = Ofac_Add()
post.number_id = row[0] #am pus un _id la foreign key k asa stokeaza django foreign key-ul
post.n = row[1]
post.adresa=row[2]
post.oras=row[3]
post.stat=row[4]
post.s=row[5]
post.save()
data2 = csv.reader(open(file2),delimiter=",") for row in data2:
if row[0] !="Number":
post = Ofac_Alt()
post.number_id = row[0]
post.ceva = row[1]
post.aka=row[2]
post.name_of_aka=row[3]
post.a=row[4]
post.save()
data3 = csv.reader(open(file3),delimiter=",") for row in data3:
if row[0] !="Number":
post = Ofac_Sdn_Comments()
post.number_id = row[0]
post.more_info = row[1]
post.save()
错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 239, in _commit
return self.connection.commit()
sqlite3.IntegrityError: FOREIGN KEY constraint failed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/Dropbox/ofac_project/ofac_sdn/import_save/import_all_sdn.py", line 52, in <module>
post.save()
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python3.6/site-packages/django/db/transaction.py", line 212, in __exit__
connection.commit()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 261, in commit
self._commit()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 239, in _commit
return self.connection.commit()
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 239, in _commit
return self.connection.commit()
django.db.utils.IntegrityError: FOREIGN KEY constraint failed
答案 0 :(得分:1)
问题在于以下几行:
data = csv.reader(open(file),delimiter=",") for row in data:
if row[0] !="Number":
post = Ofac_Sdn()
post.number = row[0]
和
data1 = csv.reader(open(file1),delimiter=",") for row in data1:
if row[0] !="Number":
post = Ofac_Add()
post.number_id = row[0]
帖子number_id
值应引用Ofac_Sdn
列上的主键;但是,您没有明确设置Ofac_Sdn.id
主键。如果您按如下方式更新第一个循环:
data = csv.reader(open(file),delimiter=",") for row in data:
if row[0] !="Number":
post = Ofac_Sdn()
post.id = row[0]
post.number = row[0]
这很可能会解决它。