使用CSV上传将Django批量上传到数据库 - 所有内容都上传,只有一个不上传

时间:2017-09-08 12:15:51

标签: python django csv file-upload

我正在尝试将一些大文件上传到我的Django DB。当我这样做时,所有文件都被上传,只有一个文件没有上传。没有错误,但没有上传。未上传的是最后一个 Ofac_Sdn_Comments 功能。请注意,没有错误,DB也没有相应更新。

如果有人可以帮助我,我会欠你很多。

请在下面找到我的型号:

string GetMACAddressviaIP(string ipAddr)
{
    string result = "";
    ostringstream ss;
    ss << "/proc/net/arp";
    string loc = ss.str();

    ifstream in(loc);
    if(!in)
    {
        printf("open %s failed\n",loc.c_str());
        return result;
    }

    string line;
    while(getline(in, line)){
        if(strstr(line.c_str(), ipAddr.c_str()))
        {
            const char *buf = strstr(line.c_str(), ":") - 2;
            int counter = 0;
            stringstream ss;
            while(counter < 17)
            {
                ss << buf[counter];
                counter++;
            }
            result = ss.str();
        }
    }

    return result;
}

这是我的批量上传功能:

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)
    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)
    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)
    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)
    class Meta:
        verbose_name_plural = "ofac_sdn_comments"
    def __str__(self):
        return ((self.number_id, self.more_info))

以下是有关csv信息的显示方式的一些信息 - 所有信息仅在实际CSV中的一行中显示(我将它放在许多行上只是为了正确看待)有问题的那个:

import csv, sys, os

project_dir = "/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/import_save/"
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 = '/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/import_save/sdn.csv'
file1 = '/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/import_save/add.csv'
file2='/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/import_save/alt.csv'
file3='/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/import_save/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=int(row[7])
        post.tonnage=int(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]  #am pus un _id la foreign key k asa stokeaza django foreign key-ul
        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]  #am pus un _id la foreign key k asa stokeaza django foreign key-ul
        post.more_info = row[1]

1 个答案:

答案 0 :(得分:1)

你忘了保存

for row in data3:
    if row[0] !="Number":
        post = Ofac_Sdn_Comments()
        post.number_id = row[0]
        post.more_info = row[1]
        # NEXT LINE
        post.save()