社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

导入和导出Django包外键CSV文件导入问题

Ray Zuchowski • 4 年前 • 1840 次点击  

我正在尝试导入学生数据的分数,但我不断收到错误消息,即导入时查询不匹配。我试图指定import将state_provide字段视为外键,但我认为我没有正确地编写它。其次,由于某种原因,我的日期字段一直未能通过空约束,我不明白为什么,因为excel文件数据的格式是正确的。我现在在模型中设置null=true来绕过。这是我的密码。谢谢你的帮助。非常感谢。

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)

这是我的管理员。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) 

下面是导入错误的屏幕截图。

enter image description here

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130195
文章 [ 1 ]  |  最新文章 4 年前