我试图为学生数据导入分数,并不断收到查询与导入不匹配的错误消息。我尝试将导入指定为将state_province字段视为外键,但我认为写得不正确。其次,由于某些原因,我的date字段一直失败null约束,这一点我不理解,因为excel文件数据格式是正确的。我现在在null=true模型中设置为bypass.Here是我的代码。我很感激你的帮助。谢谢
STUDENT MODEL
class Student(models.Model):
studentpsid= models.CharField(primary_key = True , default = "", max_length = 50, unique = True)
student_name = models.CharField(max_length = 50)
first_name = models.CharField(max_length = 50, default = "")
last_name = models.CharField(max_length = 50,default = "")
gender = models.CharField(max_length = 1,default = "")
birth_date = models.DateField(blank= True)
student_grade = models.CharField(max_length = 2, default = "")
home_room = models.CharField(max_length = 5, default = "")
student_enrollment = models.CharField(max_length = 2, default = "")
school_number = models.CharField(max_length = 15, default = "")
email = models.EmailField(default = "")
projected_graduation_year = models.CharField(max_length = 4, default = "")
counseling_goal = models.TextField(max_length = 500, blank = True)
win_username = models.CharField(max_length = 50, blank=True)
win_password = models.CharField(max_length = 50, blank=True)
offsite_laptop = models.BooleanField(default = False, blank = True)
state_province = models.CharField(max_length = 50, unique=True,null=True,blank = True , default = None)
lunch_balance = models.FloatField(blank = True, default = 0)
race = models.CharField(max_length = 1, blank = True)
alert_medical = models.TextField(max_length = 500, blank =True)
district_code = models.ForeignKey(NJDistictCodes,on_delete = models.PROTECT, blank = True, default = 0)
image = models.ImageField(default ="default.png", upload_to ='student_pics')
class Meta:
verbose_name_plural = "Student"
def __str__(self):
return self.student_name
TEST SCORE MODEL
class MindPrint(models.Model):
state_province = models.ForeignKey(Student, to_field="state_province" ,on_delete = models.CASCADE, default = None,null=True,blank = True)
test_date = models.DateField(null=True)
reasoning_verbal = models.CharField(max_length = 1, default = 0, blank = True)
reasoning_spatial = models.CharField(max_length = 1, default = 0, blank = True)
reasoning_abstract = models.CharField(max_length = 1, default = 0, blank = True)
executive_flexibility = models.CharField(max_length = 1, default = 0, blank = True)
executive_working_memory = models.CharField(max_length = 1, default = 0, blank = True)
executive_attention = models.CharField(max_length = 1, default = 0, blank = True)
speed_processing = models.CharField(max_length = 1, default = 0, blank = True)
speed_visual_motor = models.CharField(max_length = 1, default = 0, blank = True)
memory_verbal = models.CharField(max_length = 1, default = 0, blank = True)
memory_visual = models.CharField(max_length = 1, default = 0, blank = True)
class Meta:
verbose_name_plural = "Mind Print"
def __str__(self):
return str(self.state_province)以下是我的Admin.py文件设置。
from inspect import Attribute
from django.contrib import admin
from import_export.admin import ImportExportActionModelAdmin
from import_export import resources, fields
from import_export.widgets import ForeignKeyWidget
from .models import Student, MindPrint
class MindPrintResource (resources.ModelResource):
class Meta:
model = MindPrint
import_id_fields = ('state_province',)
state_province = fields.Field(column_name='state_province', attribute='state_province',widget=ForeignKeyWidget(Student, 'state_province'))
fields = ('state_province','test_date','reasoning_verbal','reasoning_spatial','reasoning_abstract','executive_flexibility','executive_working_memory',
'executive_attention','speed_processing','speed_visual_motor','memory_verbal','memory_visual')
class MindPrintAdmin (ImportExportActionModelAdmin):
resource_class = MindPrintResource
list_display = ('state_province_id','student_name','test_date','reasoning_verbal','reasoning_spatial','reasoning_abstract','executive_flexibility','executive_working_memory',
'executive_attention','speed_processing','speed_visual_motor','memory_verbal','memory_visual')
def student_name(self,obj):
return obj.state_province.student_name
admin.site.register(MindPrint,MindPrintAdmin) 以下是导入错误的屏幕截图。

发布于 2022-02-27 18:51:08
您已经配置了MindPrint模型对象的导入。当您导入源数据文件时,它希望有一个名为state_province的字段,它是对Student模型的FK引用。由于这是一个FK关系,因此导入过程期望存在此引用。在您的示例中,Student资源与state_province of 1234567899不存在,因此您将看到Student.DoesNotExist错误。
您可以使用几个API挂钩来检查缺少的Student行,如果它们不存在,则创建它们。或者,您可以重写ForeignKeyWidget以在查找之前创建资源。
https://stackoverflow.com/questions/71268087
复制相似问题