0
class ProductResource(resources.ModelResource):
    class Meta:
        model = Product
        exclude = ('pub_date','modified_date',)
        import_id_fields = ('CD-SKU',)

class ProductAdmin(ImportExportModelAdmin):
        resource_class = ProductResource

错误是:

Line number: 1 - u'CD-SKU'
0H041501, VOL3380235300029, 3380235300029, B00FIZJI0K, 11.59, None, None, None, None, None, None, None, None, None
Traceback (most recent call last):
File "/home/mws/Env/mws_prod/local/lib/python2.7/site-packages/import_export/resources.py", line 434, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/home/mws/Env/mws_prod/local/lib/python2.7/site-packages/import_export/resources.py", line 258, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/home/mws/Env/mws_prod/local/lib/python2.7/site-packages/import_export/resources.py", line 252, in get_instance
return instance_loader.get_instance(row)
File "/home/mws/Env/mws_prod/local/lib/python2.7/site-packages/import_export/instance_loaders.py", line 31, in get_instance
field = self.resource.fields[key]
KeyError: u'CD-SKU'

在阅读了相关问题后,我删除了本机自动生成的 id 并使用我自己的字段作为主要字段。在 Resource 类中提到了它。但它仍然无法导入。

将不胜感激所有的帮助。谢谢!

4

2 回答 2

2

看起来你只是有一个错字。你的模型有什么CD_SKU和你的资源使用CD-SKU

class Product(models.Model):
    CD_SKU = models.CharField(max_length = 200, primary_key=True) 

对比

import_id_fields = ('CD-SKU',)

将其更改为:

import_id_fields = ('CD_SKU',)

希望能帮助到你!

于 2017-07-24T06:05:33.987 回答
0

在对类似问题进行故障排除后遇到了这个问题。

import_id_fields对我来说,解决方法是在声明中包含尾随逗号。希望这可以帮助其他任何人搜索这些"Line number: 1" 错误。

于 2019-11-23T21:20:34.650 回答