I am trying to import some csv data into a postgresql database using django-import-export. I am using python 3.4, django 1.8.1, and import-export 0.5.1.
My settings file has the following directive:
IMPORT_EXPORT_USE_TRANSACTIONS = True
my model has 10 fields. Postgresql autoincrements an 11th field, the 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
In admin.py I whitelist 8 fields, including the id field:
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')
My shell commands come right out of 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'])
The file I am trying to import, test.csv, looks like this:
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
Here is the traceback:
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
Because of testing, the latest id in the database model is 21, so the next auto-increment should be 22.