Django错误 - ValueError:无法分配"' int":"行名称"必须是" ForeignKey"例

时间:2017-09-05 19:18:12

标签: python django csv

我得到了这个奇怪的错误,我不知道为什么。

ValueError: Cannot assign "'36'": "Ofac_Add.number" must be a "Ofac_Sdn" instance.

我创建了Django模型,它们完美运行。请在下面找到我的型号:

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)
    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)
    # content = models.TextField(blank=True, null=True, default="")

    class Meta:
        verbose_name_plural = "ofac_add"

    def __str__(self):
        return self.number, self.adresa ,  self.oras_zip


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)
    # content = models.TextField(blank=True, null=True, default="")

    class Meta:
        verbose_name_plural = "ofac_alt"

    def __str__(self):
        return self.number,  self.aka, self.name_of_aka

我已将批量CSV文件上传到我的第一个模型,现在尝试将第二个CSV文件上传到与第一个模型有关系的第二个模型。当我这样做时,我收到错误*ValueError: Cannot assign "'36'": "Ofac_Add.number" must be a "Ofac_Sdn" instance.*此CSV文件中的行如下所示:

Number,N,Adresa,Oras,Stat,S
36,25,null,Havana,Cuba,null

我的第一张表中的信息是这样的:

Number,Name,B/I,Program,More Info,Vessel CallSign,Vessel Type,Vessel DWT (Deadweight tonnage),Gross Registered Tonnage,Vessel Flag,Vessel Owner,DOB/AKA
36,AEROCARIBBEAN AIRLINES,null,CUBA,null,null,null,0,0,null,null,null

所以外键进入第一个表。 导入功能就是这个:

import csv, sys, os

project_dir = "/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/"

sys.path.append(project_dir)

os.environ['DJANGO_SETTINGS_MODULE']='ofac.settings'

import django
django.setup()

from ofac_sdn.models import Ofac_Add

data = csv.reader(open('/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/add.csv'),delimiter=",")
#data = csv.reader(open('/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/sdn2.csv'), dialect='excel-tab')
for row in data:

    if row[0] !="Number":
        post = Ofac_Add()
        post.number = row[0]
        post.n = row[1]
        post.adresa=row[2]
        post.oras=row[3]

        post.stat=row[4]
        post.s=row[5]

        post.save()
        #
        # try:
        #     int('')
        # except ValueError:
        #     pass

如果有人可以提供帮助,我会欠你很多!

谢谢你, 科恩

1 个答案:

答案 0 :(得分:1)

对不起我的英语我是外国人。

您应该更改您阅读csv的文件 来自

post.number

要    post.number_id

因为 post.number 只接受 ofec_sdn 实例 并指定一个id为id的号码。 ofec_sdn.number_id是db上django存储外键的行