简体   繁体   English

用django-adaptors导入csv文件

[英]importing csv file with django-adaptors

I'm trying to import csv file with django-adaptors , unfortunately i have an error in this process. 我正在尝试使用django-adaptors导入csv文件,不幸的是我在此过程中出错。

I created my model and CSV class like this: 我创建了我的模型和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

my csv file looks like this: 我的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

You can see that i have extra Field im model "iddepts" which is primary key, exluding it in MyCsvModel dosent slove problem. 您可以看到,我有额外的Field im模型“ iddepts”,这是主键,将其排除在MyCsvModel剂量问题之外。 i have still error on import_data. 我在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

Thanks in advance. 提前致谢。

Using a CsvDbModel means your django model fields and CSV columns match. 使用CsvDbModel意味着您的Django模型字段和CSV列匹配。 Your CSV data lacks a column for iddepts. 您的CSV数据缺少iddepts列。

Therefore you need to create a django-adaptors CsvModel. 因此,您需要创建一个django-adaptors CsvModel。 And you need to tell DateField how a date is formatted. 您需要告诉DateField日期的格式。 In your CSV file: 在您的CSV文件中:

'%Y-%m-%d' '%Y-%间 - %d'

django-adaptors defaults it to : django-adaptors 默认为

'%d/%m/%Y' '%d /%米/%Y'

Set up this model. 设置此模型。 If field names of your Csv model does not match the field names of your django model, you can manage this with the match keyword 如果Csv模型的字段名称与django模型的字段名称不匹配,则可以使用match关键字进行管理

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

Using this model you can import CSV file using django shell. 使用此模型,您可以使用django shell导入CSV文件。 I did successfully import your CSV data into SQL database table. 我确实将您的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>]

Customization option: You can even create custom fields matching date formats in your CSV data. 自定义选项:您甚至可以在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM