【问题标题】:importing csv file with django-adaptors使用 django-adaptors 导入 csv 文件
【发布时间】:2014-01-28 15:08:16
【问题描述】:

我正在尝试使用 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', 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

我的 csv 文件如下所示:

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 问题中排除了它。我在 import_data 上仍有错误。

>>>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

提前致谢。

【问题讨论】:

    标签: python django csv django-adaptors


    【解决方案1】:

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

    因此您需要创建一个 django-adaptors CsvModel。您需要告诉 DateField 日期的格式。在您的 CSV 文件中:

    '%Y-%m-%d'

    django 适配器defaults it to:

    '%d/%m/%Y'

    设置此模型。如果您的 Csv 模型的字段名称与您的 django 模型的字段名称不匹配,您可以manage this with the match keyword

    from adaptor.model import CsvModel
    from adaptor.fields import CharField, IntegerField, DecimalField, DateField
    
    from application.models import depts      
    
    class DeptsCsvModel(CsvModel):
    
        CustomerID = IntegerField()
        CustomerName = CharField()
        InvoiceID = IntegerField()
        Currency = CharField()
        CurrencyRate = DecimalField() 
        PriceWithoutVAT = DecimalField()
        PriceWithVAT = DecimalField()
        VAT = DecimalField()
        CustomerVATID = CharField()
        CustomerAddress = CharField()
        InvoiceBranch = IntegerField()
        InvoiceVATType = IntegerField()
        InvoiceDateCreate = DateField(**{'format':'%Y-%m-%d'})
        InvoiceDateDue = DateField(**{'format':'%Y-%m-%d'})
        InvoiceCodeVAT = CharField()
        PriceWithoutVATStandard = DecimalField()
        PriceWithVATStandard = DecimalField()
        VATStandard = DecimalField()
        AccountVAT = CharField()
        AccountPriceWithoutVAT = CharField()
        AccountPriceWithVAT = CharField()
    
        class Meta:
            dbModel = depts
            delimiter = ";"
            has_header = True
    

    使用此模型,您可以使用 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>]
    

    自定义选项:您甚至可以在 CSV 数据中创建与日期格式匹配的自定义字段。

    class DeptsDateField(DateField):
        "encapsulate specific data properties into a class for use with django-adaptors CsvModel fields"
    
        def __init__(self):
            super(DeptsDateField, self).__init__(**{'format':'%Y-%m-%d'})
    
    class DeptsCsvModel(CsvModel):
    
        CustomerID = IntegerField()
        CustomerName = CharField()
        InvoiceID = IntegerField()
        Currency = CharField()
        CurrencyRate = DecimalField() 
        PriceWithoutVAT = DecimalField()
        PriceWithVAT = DecimalField()
        VAT = DecimalField()
        CustomerVATID = CharField()
        CustomerAddress = CharField()
        InvoiceBranch = IntegerField()
        InvoiceVATType = IntegerField()
        InvoiceDateCreate = DeptsDateField
        InvoiceDateDue = DeptsDateField
        InvoiceCodeVAT = CharField()
        PriceWithoutVATStandard = DecimalField()
        PriceWithVATStandard = DecimalField()
        VATStandard = DecimalField()
        AccountVAT = CharField()
        AccountPriceWithoutVAT = CharField()
        AccountPriceWithVAT = CharField()
    
        class Meta:
            dbModel = depts
            delimiter = ";"
            has_header = True
    

    【讨论】:

    • 我尝试了您的解决方案,现在导入作品,但日期字段不正确,每条记录都有当前日期。
    • 这是因为你的 Django DateFields 就像 models.DateField(auto_now=True)。这会丢弃使用 django-adaptors 提供的任何值。从文档中得到它。我建议仅将 django 模型用于 CSV 导入,而不将 auto_now 用于 DateFields 以及您为其他所有应用程序上下文提供的模型。
    • 当我插入它时,它显示错误并告诉“没有名为'应用程序'的模块”-如果你能告诉我该怎么办
    • 除非您维护旧的 Django 安装,否则我建议更改框架以将 CSV 数据导入 Django。 django-adaptors 相当陈旧且无人维护,Django 也发生了变化。为什么要花时间在 django-adaptors 这样的僵尸项目上?
    • @SaschaGottfried - 不确定我是否可以在这里问 - 正在探索将遗留数据导入 Django 模型的各种选项。在没有明确来源的情况下,我遇到了 django-adaptors 并认为它适合我的目的。但是,看到您的评论,我现在不太确定。如果可能的话,您能否指导我找到一个可能的解决方案(我在 python 3.6 上使用 Django 版本 2.0.6)。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多