我需要幫助才能將CSV上傳到我的模型。我看到還有其他一些問題,但它們很老,或者用2.7編寫,並沒有任何意義。將CSV導入到我的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)
last_name= 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)
這是從我的CSV行的模式:
36,AEROCARIBBEAN AIRLINES,-0- ,CUBA,-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,-0-
我曾嘗試到現在這個例子,但是我收到一個錯誤說:ModuleNotFoundError:沒有名爲「設置」模塊
如果有人可以幫忙,我會因爲我被卡在這裏而欠你很多!
謝謝!
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']='settings'
import django
django.setup()
from ofac_sdn.models import Ofac_Sdn
data = csv.reader(open('/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/sdn.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_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.tonnage=row[8]
post.vessel_flag=row[9]
post.vessel_owner=row[10]
post.dob_aka=row[11]
post.save()
謝謝!有關於如何使用此導入導出模塊的視頻教程? – Cohen
我從來沒有使用視頻教程,但你很好[文檔在這裏](http://django-import-export.readthedocs.io/en/stable/) –