我正在尝试将 excel 文档导入 Django DB。我已将以下代码添加到admin.py
和model.py
. Django的开发似乎有错误。我已阅读有关如何修复此错误的几个不同文档。但是我仍然对如何准确实施它有点迷茫。
在 Trace 中,它一直说我的 excel 文档需要一个id
字段。我的 excel 文档中没有id
字段,我也没有告诉我的模型寻找id
字段。
我发现的文档说明我应该get_or_init_instance
在这里使用:
https://django-import-export.readthedocs.org/en/latest/import_workflow.html
你们能提供的任何帮助都会很棒。
管理员.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')
模型.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',)
痕迹:
> 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)