[英]Import a CSV to my Django Models
I need help in order to upload a CSV to my models. 我需要帮助才能将CSV上传到我的模型。 I saw that there are some other questions, but they are old, or written in 2.7 and doesn't make no sense.
我看到还有其他问题,但是它们很老,或者是用2.7编写的,没有任何意义。
These are my models. 这些是我的模特。
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)
This is the model of a row from my CSV: 这是我的CSV中的一行模型:
36,AEROCARIBBEAN AIRLINES,-0- ,CUBA,-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,-0- ,-0-
I have tried until now this example, but i receive an error saying: ModuleNotFoundError: No module named 'settings' 到目前为止,我一直在尝试此示例,但是我收到一条错误消息: ModuleNotFoundError:没有名为“ settings”的模块
If someone could help, I would owe you a lot as i am stuck here! 如果有人可以帮忙,我会被困在这里,我欠你很多!
Thank you! 谢谢!
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()
The error has nothing to do with CSV importing. 该错误与CSV导入无关。
The error is saying there is no such module with the path settings
. 错误是说路径
settings
没有这样的模块。 This means that the you put the wrong path here: 这意味着您在这里输入了错误的路径:
os.environ['DJANGO_SETTINGS_MODULE']='settings'
It should be project_name.settings
or the path that fits your project. 它应该是
project_name.settings
或适合您的项目的路径。
Hope it helps. 希望能帮助到你。
i think you need add you project name: 我认为您需要添加您的项目名称:
os.environ['DJANGO_SETTINGS_MODULE']='project_name.settings'
# ^^^^^^^^^^^^
and you can look for django-import-export it can be simple solution 并且您可以寻找django-import-export这可能是简单的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.