I am trying to import excel documents into a Django DB. I have added the following code to admin.py
and model.py
. There seems to be an error in the development of Django. I have read through several different documentations about how to fix this error. But I am still a little lost on how to implement it exactly.
In the Trace it keeps saying that my excel document needs an id
field. There is no id
field in my excel docs nor did I tell my model to look for an id
field.
The documentation that I have found states that I should use get_or_init_instance
here:
https://django-import-export.readthedocs.org/en/latest/import_workflow.html
Any help that you guys could give would be great.
admin.py
class VinCasesAndCampaignsResource(resources.ModelResource):
published = fields.Field(column_name='published_date')
def get_instance(self, instance_loaders, row):
return False
class Meta:
model = VinCasesAndCampaigns
widgets = {}
fields = ('VIN','LatestOpenCaseID','LatestClosedCaseID',
'OpenDate', 'CloseDate', 'HasCampaigns',)
import_id_fields = ['VIN']
export_order = ('VIN',)
exclude = ('id')
model.py
class VinCasesAndCampaigns(models.Model):
VIN = models.CharField(max_length=30)
LatestOpenCaseID = models.DateField()
LatestClosedCaseID = models.DateField()
OpenDate = models.DateField()
CloseDate = models.DateField()
HasCampaigns = models.BooleanField(default = False)
HasOpenCampaigns = models.BooleanField(default = False)
HasCases = models.BooleanField(default = False)
HasEstimates = models.BooleanField(default = False)
HasDwell = models.BooleanField(default = False)
HasClaims = models.BooleanField(default = False)
exclude = ('id',)
Trace:
> Line number: 1 - u"Column 'id' not found in dataset. Available columns
> are: [u'VIN', u'LatestOpenCaseID', u'LatestClosedCaseID', u'OpenDate',
> u'CloseDate', u'HasCampaigns', u'HasOpenCampaigns', u'HasCases',
> u'HasEstimates', u'HasDwell', u'HasClaims']" Traceback (most recent
> call last): File
> "/Users/USER/anaconda/lib/python2.7/site-packages/django_import_export-0.2.8.dev0-py2.7.egg/import_export/resources.py",
> line 342, in import_data instance, new =
> self.get_or_init_instance(instance_loader, row)
You can solve this problem by using the following steps:
Step 1 first goto C:/Users/am.sa18/Desktop/myenv/Lib/site-packages/import_export/resources.py
Step 2 open resources.py in the editor.
Step 3 do change in line no 84, its looks like this
Step 4 import_id_fields = ['id'] Where 'id' by-default set by import_export package, you can change 'id' by your primary key of the model. After adding primary key : import_id_fields = ['primary_key']
Step 5 Save resources.py file and run the server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With