Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用django适配器导入csv文件_Python_Django_Csv_Django Adaptors - Fatal编程技术网

Python 使用django适配器导入csv文件

Python 使用django适配器导入csv文件,python,django,csv,django-adaptors,Python,Django,Csv,Django Adaptors,我正在尝试使用导入csv文件,不幸的是,我在此过程中出错 我创建的模型和CSV类如下: class depts(models.Model): iddepts = models.AutoField(primary_key=True) CustomerID = models.IntegerField(default=0, null=False) CustomerName = models.CharField(max_length=750, default='none', nu

我正在尝试使用导入csv文件,不幸的是,我在此过程中出错

我创建的模型和CSV类如下:

class depts(models.Model):
    iddepts = models.AutoField(primary_key=True)
    CustomerID = models.IntegerField(default=0, null=False)
    CustomerName = models.CharField(max_length=750, default='none', null=False)
    InvoiceID = models.IntegerField(max_length=20, default=0, null=False)
    Currency = models.CharField(max_length=3, default='EUR', null=False)
    CurrencyRate = models.DecimalField(max_digits=5, decimal_places=2) 
    PriceWithoutVAT = models.DecimalField(max_digits=20, decimal_places=2)
    PriceWithVAT = models.DecimalField(max_digits=20, decimal_places=2)
    VAT = models.DecimalField(max_digits=20, decimal_places=2)
    CustomerVATID = models.CharField(max_length=35, default='000-000-000-000', null=False)
    CustomerAddress = models.CharField(max_length=750, default='none', null=False)
    InvoiceBranch = models.IntegerField(max_length=3, default='000', null=False)
    InvoiceVATType = models.IntegerField(max_length=1, default='1', null=False)
    InvoiceDateCreate = models.DateField(auto_now=True)
    InvoiceDateDue = models.DateField(auto_now=True)
    InvoiceCodeVAT = models.CharField(max_length=20, default='none', null=False)
    PriceWithoutVATStandard = models.DecimalField(max_digits=20, decimal_places=2)
    PriceWithVATStandard = models.DecimalField(max_digits=20, decimal_places=2)
    VATStandard = models.DecimalField(max_digits=20, decimal_places=2)
    AccountVAT = models.CharField(max_length=20, default='none', null=False)
    AccountPriceWithoutVAT = models.CharField(max_length=20, default='none', null=False)
    AccountPriceWithVAT = models.CharField(max_length=20, default='none', null=False)
    class Meta:
            verbose_name = "Invoice"
            verbose_name_plural = "Invoices"
    def __str__(self):
        return self.InvoiceID        
    def __unicode__(self):
        return self.InvoiceID
class MyCsvModel(CsvDbModel):
    class Meta:
          dbModel = depts
          delimiter = ";"
          has_header = True
CustomerID;CustomerName;InvoiceID;Currency;CurrencyRate;PriceWithoutVAT;PriceWithVAT;VAT;CustomerVATID;CustomerAddress;InvoiceBranch;InvoiceVATType;InvoiceDateCreate;InvoiceDateDue;InvoiceCodeVAT;PriceWithoutVATStandard;PriceWithVATStandard;VATStandard;AccountVAT;AccountPriceWithoutVAT;AccountPriceWithVAT
73269;Good CO;131002919;EUR;1;141.12;173.58;32.46;666-666-11-11;Good street 123;002;1;2013-04-15;2013-04-22;21% ;141.12;173.58;32.46;111-111;111-111-111;111-111-11111
我的csv文件如下所示:

class depts(models.Model):
    iddepts = models.AutoField(primary_key=True)
    CustomerID = models.IntegerField(default=0, null=False)
    CustomerName = models.CharField(max_length=750, default='none', null=False)
    InvoiceID = models.IntegerField(max_length=20, default=0, null=False)
    Currency = models.CharField(max_length=3, default='EUR', null=False)
    CurrencyRate = models.DecimalField(max_digits=5, decimal_places=2) 
    PriceWithoutVAT = models.DecimalField(max_digits=20, decimal_places=2)
    PriceWithVAT = models.DecimalField(max_digits=20, decimal_places=2)
    VAT = models.DecimalField(max_digits=20, decimal_places=2)
    CustomerVATID = models.CharField(max_length=35, default='000-000-000-000', null=False)
    CustomerAddress = models.CharField(max_length=750, default='none', null=False)
    InvoiceBranch = models.IntegerField(max_length=3, default='000', null=False)
    InvoiceVATType = models.IntegerField(max_length=1, default='1', null=False)
    InvoiceDateCreate = models.DateField(auto_now=True)
    InvoiceDateDue = models.DateField(auto_now=True)
    InvoiceCodeVAT = models.CharField(max_length=20, default='none', null=False)
    PriceWithoutVATStandard = models.DecimalField(max_digits=20, decimal_places=2)
    PriceWithVATStandard = models.DecimalField(max_digits=20, decimal_places=2)
    VATStandard = models.DecimalField(max_digits=20, decimal_places=2)
    AccountVAT = models.CharField(max_length=20, default='none', null=False)
    AccountPriceWithoutVAT = models.CharField(max_length=20, default='none', null=False)
    AccountPriceWithVAT = models.CharField(max_length=20, default='none', null=False)
    class Meta:
            verbose_name = "Invoice"
            verbose_name_plural = "Invoices"
    def __str__(self):
        return self.InvoiceID        
    def __unicode__(self):
        return self.InvoiceID
class MyCsvModel(CsvDbModel):
    class Meta:
          dbModel = depts
          delimiter = ";"
          has_header = True
CustomerID;CustomerName;InvoiceID;Currency;CurrencyRate;PriceWithoutVAT;PriceWithVAT;VAT;CustomerVATID;CustomerAddress;InvoiceBranch;InvoiceVATType;InvoiceDateCreate;InvoiceDateDue;InvoiceCodeVAT;PriceWithoutVATStandard;PriceWithVATStandard;VATStandard;AccountVAT;AccountPriceWithoutVAT;AccountPriceWithVAT
73269;Good CO;131002919;EUR;1;141.12;173.58;32.46;666-666-11-11;Good street 123;002;1;2013-04-15;2013-04-22;21% ;141.12;173.58;32.46;111-111;111-111-111;111-111-11111
您可以看到,我有一个额外的字段im模型“iddepts”,它是主键,将其排除在MyCsvModel dosent slove问题中。导入_数据时仍有错误

>>>from app.models import MyCsvModel
>>>my_csv_list = MyCsvModel.import_data(data = open("file.csv"))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sitepackages/adaptor/model.py", line 197, in import_data
return importer.import_data(data)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/adaptor/model.py", line 467, in import_data
self.process_line(data, line, lines, line_number, self.csvModel)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/adaptor/model.py", line 487, in process_line
raise CsvDataException(line_number, error="Number of fields invalid")
CsvDataException: Line 2: Number of fields invalid
>>从app.models导入MyCsvModel
>>>my_csv_list=MyCsvModel.import_数据(数据=打开(“file.csv”))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sitepackages/adapter/model.py”,第197行,在import_数据中
返回导入器。导入_数据(数据)
文件“/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/adapter/model.py”,第467行,在import_data中
self.process_行(数据、行、行、行编号、self.csvModel)
文件“/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/adapter/model.py”,第487行,进程中
引发CsvDataException(行号,错误=“字段数无效”)
CsvDataException:第2行:字段数无效

提前感谢。

使用CsvDbModel意味着您的django模型字段和CSV列匹配。您的CSV数据缺少IDDept列

因此,您需要创建django Adapters CsvModel。您需要告诉DateField日期是如何格式化的。在您的CSV文件中:

“%Y-%m-%d”

django适配器:

“%d/%m/%Y”

建立这个模型。如果Csv模型的字段名与django模型的字段名不匹配,则可以

使用此模型,您可以使用django shell导入CSV文件。我成功地将您的CSV数据导入SQL数据库表

gottfried@lubuntu-virtual-machine:~/virtualenvs/django-adaptors/soquestion/application$ ../../bin/python ../manage.py shell
Python 2.7.2+ (default, Jul 20 2012, 22:12:53) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from application.adaptors import DeptsCsvModel
>>> DeptsCsvModel.import_data(data = open('data.csv'))
[<application.adaptors.DeptsCsvModel object at 0x93efa8c>]

我尝试了您的解决方案,现在导入工作正常,但datefield不正确,每个记录都有当前日期。这是因为您的Django datefield类似于models.datefield(auto_now=True)。这会丢弃使用django适配器提供的任何值。从医生那里得到的。我建议只为CSV导入使用django模型,而不为DateFields使用auto_now,也不为其他所有应用程序上下文使用您提供的auto_。当我插入此模型时,它会显示错误并告诉“没有名为“application”的模块”-如果您能告诉我,我该怎么办,除非您维护旧的django安装,我建议更改框架,将CSV数据导入Django。django适配器比较旧,没有维护,django也发生了变化。为什么要在django Adapters这样的僵尸项目上投入时间?@saschagotfried——我不确定是否可以在这里问一下——正在探索将遗留数据导入django模型的各种选项。在没有确切来源的情况下,我遇到了django适配器,并认为它符合我的目的。然而,看到你的评论,我现在不太确定。如果可能的话,您能告诉我一个可能的解决方案吗(我正在Python3.6上使用Django版本2.0.6)。谢谢