在我的项目中,我想每年招收300名学生。因此,无法使用添加学生表格。因此,我想使用csv或excel文件创建用于批量数据输入的功能。我已经尝试了许多与此相关的事情,但无法解决。
** models.py **
class AddStudent(models.Model):
enrollment_no = models.BigIntegerField(primary_key=True)
student_name = models.CharField(max_length=500,null=True)
gender = models.CharField(max_length=1,choices=GENDER_CHOICES)
course = models.ForeignKey(CourseMaster, on_delete=models.DO_NOTHING, null=True)
category= models.ForeignKey(CatMaster, on_delete=models.DO_NOTHING, null=True)
admission_year = models.IntegerField(('year'), choices=YEAR_CHOICES, default=datetime.datetime.now().year)
college = models.ForeignKey(CollegeMaster, on_delete=models.DO_NOTHING, null=True)
branch = models.ForeignKey(BranchMaster,on_delete=models.DO_NOTHING, null=True)
current_semester = models.IntegerField(null=True)
address = models.CharField(max_length=1000,null=True)
city = models.CharField(max_length=100,null=True)
district = models.CharField(max_length=100,null=True)
state = models.CharField(max_length=100,null=True)
student_contact = models.BigIntegerField()
parent_contact = models.BigIntegerField()
这是我的models.py文件,我想通过csv文件存储以下字段。有些字段是与其他模型相关的外键。那么如何解决呢?
** Views.py **
def upload_csv(request):
data = {}
if "GET" == request.method:
return render(request, "add_student/bulk.html", data)
# if not GET, then proceed
try:
csv_file = request.FILES["csv_file"]
if not csv_file.name.endswith('.csv'):
messages.error(request,'File is not CSV type')
return HttpResponseRedirect(reverse("add_student:upload_csv"))
#if file is too large, return
if csv_file.multiple_chunks():
messages.error(request,"Uploaded file is too big (%.2f MB)." % (csv_file.size/(1000*1000),))
return HttpResponseRedirect(reverse("add_student:upload_csv"))
file_data = csv_file.read().decode("utf-8")
lines = file_data.split("\n")
#loop over the lines and save them in db. If error , store as string and then display
for line in lines:
fields = line.split(",")
data_dict = {}
data_dict["enrollment_no"] = fields[0]
data_dict["student_name"] = fields[1]
data_dict["gender"] = fields[2]
data_dict["course"] = fields[3]
data_dict["category"] = fields[4]
data_dict["admission_year"] = fields[5]
data_dict["branch"] = fields[6]
data_dict["current_semester"] = fields[7]
data_dict["address"] = fields[8]
data_dict["city"] = fields[9]
data_dict["district"] = fields[10]
data_dict["state"] = fields[11]
data_dict["student_contact"] = fields[12]
data_dict["parent_contact"] = fields[13]
try:
form = EventsForm(data_dict)
if form.is_valid():
form.save()
else:
logging.getLogger("error_logger").error(form.errors.as_json())
except Exception as e:
logging.getLogger("error_logger").error(repr(e))
pass
except Exception as e:
logging.getLogger("error_logger").error("Unable to upload file. "+repr(e))
messages.error(request,"Unable to upload file. "+repr(e))
return HttpResponseRedirect(reverse("add_student:upload_csv"))
urls.py
path('upload/csv/', views.upload_csv, name='upload_csv'),
我已经从Internet尝试了此示例,但此方法不起作用。请提出可能的解决方案。如果对此提供一些示例,请分享。请分享一些简单的解决方案,因为我是django的新手。
答案 0 :(得分:0)
我必须为我的一个项目执行此操作。我这样做的方法是创建两个模型。一个定义学生,另一个定义要导入的文件。然后,如果使用该选项批量导入CSV文件,则必须创建一个后保存钩子。
models.py
import os
import csv
from django.db import models
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.conf import settings
class Student(models.Model):
fname = models.CharField(max_length=32,
blank=False,
null=False)
lname = models.CharField(max_length=32,
blank=False,
null=False)
... # additional model attributes
class StudentImportFile(models.Model):
# upload to MEDIA_ROOT/temp
student_import = models.FileField(upload_to="temp",
blank=False,
null=False)
def save(self, *args, **kwargs):
if self.pk:
old_import = StudentImportFile.objects.get(pk=self.pk)
if old_import.student_import:
old_import.student_import.delete(save=False)
return super(StudentImportFile, self).save(*args, **kwargs)
# post save signal
@receiver(post_save, sender=StudentImportFile, dispatch_uid="add_records_to_student_from_import_file")
def add_records_to_student_from_import_file(sender, instance, **kwargs):
to_import = os.path.join(settings.MEDIA_ROOT, instance.student_import.name)
with open(to_import) as f:
reader = csv.DictReader(f)
for row in reader:
fname = row['First Name']
lname = row['Last Name']
... # additional fields to read
s = Student(fname=fname,
lname=lname,
... # additional attributes
)
s.save()