我正在尝试使用csv import-export包导入三个数据集的三个数据集:服务,库和价格。当我尝试上传服务模型的数据集时出现此错误。
Line number: 1 - foreign key mismatch - "catalog_price" referencing "catalog_service"
如果我尝试加价,则会出现此错误。库模型加载良好。
Line number: 1 - Service matching query does not exist.
models.py 共有三种模型:服务,库和价格。
class Service(models.Model):
serviceid = models.UUIDField(default=uuid.uuid4, help_text='Unique ID for this particular service in database')
desc_us = models.TextField(blank=True, primary_key = True)
cpt = models.IntegerField(default= 10000)
price = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
class Library(models.Model):
hid = models.CharField(max_length = 8, null=True)
name = models.CharField(max_length=200, primary_key=True)
hopid = models.UUIDField(default=uuid.uuid4, help_text='Unique ID for this particular library in database')
address = models.CharField(max_length = 200, null = True)
city = models.CharField(max_length = 50, null = True)
state = models.CharField(max_length = 2, null=True)
zipcode = models.CharField(max_length = 5, null=True)
phone = models.CharField(max_length = 12, null=True)
updateDate = models.DateField(blank=True, null=True)
class Price(models.Model):
priceid = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this particular service in database')
com_desc = models.CharField(max_length = 200, blank = True, null = True)
service = models.ForeignKey("Service", on_delete=models.SET_NULL, null=True)
price_offer = models.DecimalField(max_digits=8, decimal_places=2, blank=True)
comments = models.CharField(max_length = 200, blank = True, null =True)
library = models.ForeignKey("Library", on_delete=models.SET_NULL, null=True)
admin.py 这是管理文件。我在这里为每个模型创建管理员。服务和价格模型不上载,但库模型上载。
from import_export.admin import ImportExportModelAdmin
from import_export import fields, resources
class ServiceResource(resources.ModelResource):
class Meta:
model = Service
report_skipped = True
exclude = ('id','serviceid')
import_id_fields = ('desc_us', 'cpt', 'price',)
@admin.register(Service)
class ServiceAdmin(ImportExportModelAdmin):
resource_class = ServiceResource
class LibraryResource(resources.ModelResource):
class Meta:
model = Library
report_skipped = True
exclude = ('id','hopid','updateDate')
import_id_fields = ('hid', 'name', 'address', 'city', 'state', 'zipcode', 'phone',)
@admin.register(Library)
class LibraryAdmin(ImportExportModelAdmin):
resource_class = LibraryResource
class PriceResource(resources.ModelResource):
library = import_export.fields.Field(column_name='library',attribute='library', widget=ForeignKeyWidget(Library, 'hid'))
service = import_export.fields.Field(column_name='service',attribute='service', widget=ForeignKeyWidget(Service, 'cpt'))
class Meta:
model = Price
report_skipped = True
exclude = ('id','priceid')
import_id_fields = ('com_desc', 'service', 'price_offer', 'comments', 'hospital', )
@admin.register(Price)
class PriceAdmin(ImportExportModelAdmin):
resource_class = PriceResource