【发布时间】:2018-09-27 12:51:14
【问题描述】:
我的 models.py 文件是
from django.db import models
class companyDetail(models.Model):
short_code = models.CharField(primary_key=True, max_length=50)
title = models.CharField(max_length=50)
page_title = models.CharField(max_length=50)
class Meta:
verbose_name = "companyDetail"
verbose_name_plural = "companyDetails"
def __str__(self):
return self.title
class companyDescription(models.Model):
comDetail = models.OneToOneField(
companyDetail,
on_delete=models.CASCADE,
related_name='coDetail',
primary_key=True,
)
description = models.CharField(max_length=5000)
add_description = models.CharField(max_length=5000)
class Meta:
verbose_name = "companyDescription"
verbose_name_plural = "companyDescriptions"
def __str__(self):
return self.comDetail.title
我试图将我的模型/表“公司”分解为两个不同的模型“companyDetail”和“companyDescription”,并通过 OneToOneField 连接它们。
当我尝试通过 shell 添加数据时,它工作正常。
我正在为模型“companyDetail”导入 csv 到 django-import-export,它工作正常,但我正在为模型“companyDescription”导入 csv,它会引发错误:
Traceback (most recent call last):
File "/home/abhirajput/testpro/myenv/lib/python3.5/site-packages/import_export/resources.py", line 453, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/home/abhirajput/testpro/myenv/lib/python3.5/site-packages/import_export/resources.py", line 267, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/home/abhirajput/testpro/myenv/lib/python3.5/site-packages/import_export/resources.py", line 261, in get_instance
return instance_loader.get_instance(row)
File "/home/abhirajput/testpro/myenv/lib/python3.5/site-packages/import_export/instance_loaders.py", line 31, in get_instance
field = self.resource.fields[key]
KeyError: 'comdetail'
请用外行的方式帮助我,因为我是一名土木工程师,试图进入 Web 开发和破坏表格(Breaking Bad)。 如果您对将模型/表“公司”分成两个模型“companyDetail”和“companyDescription”有任何其他建议,请告诉我。
提前致谢
【问题讨论】:
标签: python django django-import-export