我正在尝试将具有一些数据的Excel文件导入到具有foreignKey
的模型中,但是得到KeyError: 'id'
我已成功导出数据并导入了相同的导出文件,当我向同一文件中添加新数据时出现了问题。
导入:
此进口商将导入以下字段:名称,品牌__品牌名称,性别,股票价格
要导入的文件: 格式: xlsx 失误 行号:1-'id' p3,B1,中性,99999
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/import_export/resources.py", line 639, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/import_export/resources.py", line 334, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/import_export/resources.py", line 321, in get_instance
import_id_fields = [
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/import_export/resources.py", line 322, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'
行号:2-'id' p2,b2,中性,99999
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/import_export/resources.py", line 639, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/import_export/resources.py", line 334, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/import_export/resources.py", line 321, in get_instance
import_id_fields = [
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/import_export/resources.py", line 322, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'
我的模特:
from django.db import models
from django.utils import timezone
from django.conf import settings
class Brand(models.Model):
BrandName= models.CharField(max_length=40)
def __str__(self):
return self.BrandName
class Perfume(models.Model):
genderChoice = (
('unisex','unisex'), ('male', 'male'), ('female', 'female'))
name = models.CharField(max_length=50)
Brand= models.ForeignKey(to=Brand,on_delete=models.CASCADE, blank=False)
gender = models.CharField(max_length=7, choices=genderChoice, default='unisex')
description = models.TextField()
stockPrice = models.IntegerField(default=99999)
active = models.BooleanField(default=False)
show_for_consumer = models.BooleanField(default=False)
created = models.DateField()
author =models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Pricing(models.Model):
product =models.ForeignKey(Perfume, on_delete=models.CASCADE,related_name='prices')
price= models.IntegerField()
volume= models.IntegerField()
def __str__(self):
return 'Perfume {} - Price{} - Volume {}'.format(self.product.name,self.price, self.volume)
我的管理员
from django.contrib import admin
from .models import Perfume, Brand, Pricing
from import_export.admin import ImportExportModelAdmin
from import_export import resources, fields
from import_export.widgets import ForeignKeyWidget
# Register your models here.
class PriceInline(admin.StackedInline):
model = Pricing
class ModelInline(admin.StackedInline):
model = Perfume
class BrandAdmin(admin.ModelAdmin):
model = Brand
inlines = [ModelInline,]
#for import settings
class ProductResorce(resources.ModelResource):
class Meta:
model = Perfume
exclude = ('active', 'show_for_consumer', 'created', 'author')
fields=('name', 'Brand__BrandName', 'gender', 'stockPrice')
export_order = ('name', 'Brand__BrandName', 'gender', 'stockPrice')
#showing in product add
class ProductImportAdmin(ImportExportModelAdmin):
resource_class = ProductResorce
list_display = ['name', 'gender', 'Brand']
fields = ['name', 'gender', 'Brand', 'author', 'created', 'stockPrice', 'description']
inlines = [PriceInline]
admin.site.register(Brand, BrandAdmin)
admin.site.register(Perfume, ProductImportAdmin)
答案 0 :(得分:0)
问题在于,当您第二次导入文件时,导入过程将尝试将每一行与数据库中的现有记录进行匹配,并尝试更新存储的数据。
默认情况下,它将使用名为“ id”的字段来执行此操作(docs)。
但是,您的数据集中或资源(ProductResorce
中没有名为“ id”的字段,因此会引发您看到的错误。
要修复它,您应该添加一个唯一标识每个条目的字段,并在资源上声明它。例如,如果您使用一个名为“ sku”的字段,则可以在您的资源上对其进行声明:
class ProductResource(resources.ModelResource):
class Meta:
model = Perfume
import_id_fields = ('sku',)
您可以使用多个字段作为唯一标识符。
拥有唯一的标识符非常有用,因为它意味着您可以安全地重新运行作业,而不会出现失败或重复的风险。