作者:披着狼皮的卷毛羊万_784 | 来源:互联网 | 2023-05-17 22:08
I'm trying to import data to one of my models, but it's failing because I'm trying to upload the foreignKey Id, not the iterated number that import-export creates.
我正在尝试将数据导入到我的某个模型中,但它失败了,因为我正在尝试上传foreignKey Id,而不是import-export创建的迭代数。
models.py
from django.db import models
from import_export import resources
class School(models.Model):
name = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
school_id = models.IntegerField(unique=True)
class Sol(models.Model):
school_id = models.ForeignKey(School, to_field='school_id')
name = models.CharField(max_length = 100)
Al1EOC = models.DecimalField(max_digits=5, decimal_places=2)
AL2EOC = models.DecimalField(max_digits=5, decimal_places=2)
#Class for django-import-export
class SolResource(resources.ModelResource):
class Meta:
model = Sol
My admin.py
from import_export.admin import ImportExportModelAdmin
class SolAdmin(ImportExportModelAdmin):
list_display = ('name', 'school_id')
resources_class = SolResource
pass
admin.site.register(Sol, SolAdmin)
My data.csv
id, name, school_id, Al1EOC, AL2EOC
,'Main st school', 1238008, 12.9, 14.9
When I export the data from the SOL model, I get an iterated number for the school ID. I want the actual School ID - the one that holds the foreignKey relationship. And, subsequently, I need to upload data with that foreignKey number. I know the ForeignKey widget is the way to do it, but I don;t understand how it is implemented.
当我从SOL模型导出数据时,我得到学校ID的迭代编号。我想要实际的学校ID - 持有foreignKey关系的那个。然后,我需要使用该foreignKey号上传数据。我知道ForeignKey小部件就是这样做的,但我不明白它是如何实现的。
3 个解决方案