我的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将它们连接起来。

当我尝试通过外壳添加数据时,它工作正常。

我将cv导入django-import-export用于模型“ companyDetail”,它工作正常,但是我将cv导入到模型“ 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”还有其他建议,请告诉我。

提前致谢

最佳答案

有一个错字:

KeyError: 'comdetail'

related_name='coDetail',

关于python - 使用OneToOneField时django-import-export键错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52537446/

10-13 07:23