我的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
我试图将模型/表“ company”分解为两个不同的模型“ companyDetail”和“ companyDescription”,并通过OneToOneField将它们连接起来。
当我尝试通过外壳添加数据时,它工作正常。
我正在将csv导入到django-import-export模型“ companyDetail”中,它可以正常工作,但是我正在将csv导入到模型“ companyDescription”中,它会引发错误:
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)。 如果您对将模型/表“ company”分为两个模型“ companyDetail”和“ companyDescription”还有其他建议,请告诉我。
预先感谢
答案 0 :(得分:0)
有一个错字:
KeyError: 'comdetail'
related_name='coDetail',
答案 1 :(得分:0)
我知道这有点太晚了。您需要在您的应用中创建一个资源文件。
class companyDescriptionResource(resources.ModelResource):
class Meta:
model = companyDescription
import_id_fields = ['comDetail']