2014-01-28 34 views
0

我想導入csv文件與django-adaptors,不幸的是我在這個過程中有一個錯誤。用Django適配器導入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 

你可以看到,我有額外的現場即時通訊模式「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 

在此先感謝。

回答

0

使用CsvDbModel表示您的django模型字段和CSV列匹配。您的CSV數據缺少iddepts列。

因此您需要創建一個django-adapters CsvModel。你需要告訴DateField一個日期的格式。在您的CSV文件:

'%Y-%間%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殼導入CSV文件。我成功地將您的CSV數據導入到SQL數據庫表中。

[email protected]:~/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 
+0

我嘗試您的解決方案,現在導入的作品,但的DateField是不正確的,每條記錄都有當前日期。 –

+0

這是因爲你的Django DateFields就像models.DateField(auto_now = True)。這拋棄了使用Django適配器提供的任何值。從文檔中獲得它。我建議僅對CSV導入使用django模型,而不是對DateFields使用auto_now以及針對每個其他應用程序上下文提供有問題的模型。 –

+0

謝謝。現在它工作 –