0

我正在尝试使用 django-import-export 将一些 csv 数据导入到 postgresql 数据库中。我正在使用 python 3.4、django 1.8.1 和 import-export 0.5.1。

我的设置文件具有以下指令:

IMPORT_EXPORT_USE_TRANSACTIONS = True

我的模型有 10 个字段。Postgresql 自动增加第 11 个字段,即 id。

class BankTransaction(models.Model):
    tacct     = models.ForeignKey(MetaAcct,on_delete=models.CASCADE)
    tdate     = models.DateTimeField('tdate')
    tctpty    = models.ForeignKey(Counterparty,on_delete=models.CASCADE)
    TRANSACTOR_CHOICES = (
        ('XXX', 'transactor1'),
        ('YYY', 'transactor2'),
    )
    tuser     = models.CharField(max_length=3,choices=TRANSACTOR_CHOICES,default="LMH")  # eg LMH
    trec      = models.IntegerField(default=0, blank=True)
    tnote     = models.CharField(default='',max_length=50, blank=True)
    tcheckno  = models.IntegerField(default=0, blank=True, null=True)
    ttype     = models.ForeignKey(TransactionType,on_delete=models.CASCADE)
    tamt      = models.DecimalField(max_digits=10, decimal_places=2) 
    tmemo     = models.CharField(default='',max_length=20, blank=True)

    def __str__(self):
        label = str.join(',',(str(self.tacct),str(self.tctpty)))
        return label

在 admin.py 中,我将 8 个字段列入白名单,包括 id 字段:

from django.contrib import admin
from import_export import resources

from .models import MetaAcct,Counterparty,TransactionType,BankTransaction

admin.site.register(MetaAcct)
admin.site.register(Counterparty)
admin.site.register(TransactionType)
admin.site.register(BankTransaction)

class MetaAcctResource(resources.ModelResource):

    class Meta:
        model = MetaAcct

class BankTransactionResource(resources.ModelResource):

    class Meta:
        model = BankTransaction
        fields = ('id','tacct','tdate','tctpty','tuser','trec','ttype','tamt')

我的 shell 命令直接来自 Read-the-docs:

>>> import tablib
>>> from import_export import resources
>>> from mny.models import BankTransaction
>>> banktransaction_resource = resources.modelresource_factory(model=BankTransaction)()
>>> dataset = tablib.Dataset(['','/Users/userid/Downloads/pa upload/test.csv'], headers=['id','tacct','tdate','tctpty','tuser','trec','ttype','tamt'])

我尝试导入的文件 test.csv 如下所示:

id,tacct,tdate,tctpty,tuser,trec,ttype,tamt
,test,2015-01-13 0:00:00,TEST_ACCT,XXX,20151031,xfer,20000
,test,2015-01-31 0:00:00,BANK_ACCT,XXX,20151031,Int Inc,0.09
,test,2015-11-30 0:00:00,BANK_ACCT,XXX,20151130,Int Inc,1.49
,test,2015-12-17 0:00:00,TEST_ACCT,XXX,20151231,xfer,12000
,test,2015-12-31 0:00:00,BANK_ACCT,XXX,20151231,Int Inc,0.2
,test,2016-01-31 0:00:00,BANK_ACCT,XXX,20160131,Int Inc,0.24
,test,2016-02-29 0:00:00,BANK_ACCT,XXX,20160229,Int Inc,0.23
,test,2016-03-31 0:00:00,BANK_ACCT,XXX,20160331,Int Inc,0.25
,test,2016-04-08 0:00:00,TEST_ACCT,XXX,20160427,xfer,-6000
,test,2016-04-30 0:00:00,BANK_ACCT,XXX,20160430,Int Inc,0.2
,test,2016-05-31 0:00:00,BANK_ACCT,XXX,20160531,Int Inc,0.2
,test,2016-06-30 0:00:00,BANK_ACCT,XXX,20160630,Int Inc,0.19
,test,2016-07-31 0:00:00,BANK_ACCT,XXX,20160731,Int inc,0.2
,test,2016-08-31 0:00:00,BANK_ACCT,XXX,20160831,Int Inc,0.2
,test,2016-09-30 0:00:00,BANK_ACCT,XXX,20160930,Int Inc,0.19
,test,2016-10-31 0:00:00,BANK_ACCT,XXX,20161031,Int Inc,0.2
,test,2016-11-30 0:00:00,BANK_ACCT,XXX,20161130,Int Inc,0.19
,test,2017-01-30 0:00:00,TEST_ACCT,XXX,20170131,xfer,15000
,test,2017-01-31 0:00:00,BANK_ACCT,XXX,20170131,Inc Inc,0.21

这是回溯:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/userid/Documents/django/testenv/lib/python3.4/site-packages/tablib/core.py", line 169, in __init__
    self.headers = kwargs.get('headers')
  File "/Users/userid/Documents/django/testenv/lib/python3.4/site-packages/tablib/core.py", line 338, in _set_headers
    self._validate(collection)
  File "/Users/userid/Documents/django/testenv/lib/python3.4/site-packages/tablib/core.py", line 286, in _validate
    raise InvalidDimensions
tablib.core.InvalidDimensions

因为测试,数据库模型中最新的id是21,所以下一次自增应该是22。

4

1 回答 1

2

看起来您可能错误地使用了 tablib数据集对象。尝试这个...

tablib.Dataset(headers=['id','tacct','tdate','tctpty','tuser','trec','ttype','tamt'])\
    .load(open('/Users/userid/Downloads/pa upload/test.csv').read())

查看教程以查看其他示例。

于 2017-04-10T14:10:29.560 回答