簡體   English   中英

如何將csv文件存儲到Django中的數據庫中?

[英]How to store csv file to database in django?

在我的項目中,我想每年招收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'),

我已經嘗試過從互聯網上這個例子,但這是行不通的。 請提出可能的解決方案。 如果對此提供一些示例,請分享。 請分享一些簡單的解決方案,因為我是django的新手。

我必須為我的一個項目執行此操作。 我這樣做的方法是創建兩個模型。 一個定義學生,另一個定義要導入的文件。 然后,如果我使用該選項批量導入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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM