【问题标题】:How to store csv file to database in django?如何将csv文件存储到django中的数据库?
【发布时间】:2019-03-03 06:48:43
【问题描述】:

在我的项目中,我想每年招收 300 名学生。所以不可能使用添加学生表格。所以我想使用 csv 或 excel 文件创建批量数据输入功能。我已经尝试了很多与此相关的事情,但无法获得解决方案。

** 模型.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 新手。

【问题讨论】:

  • 我不确定这是否是个好主意。通常对于大数据,最好将其存储在文件中。参见例如; blog.lick-me.org/2013/01/…
  • 您可以使用_id 为FK 设置值,例如course_id
  • @WillemVanOnsem 将关系数据存储在文件中是个好主意吗?您链接到的博客专门讨论将 blob/文件存储到数据库列中,而这个问题询问有关存储 CSV 行的问题。
  • 不要尝试自己实现它,而是查看可以从 CSV 文件导入和导出到 CSV 文件的 3rd 方包,例如 django-import-export
  • @Selcuk:嗯......不知何故,我不清楚这些行是否已处理。我的想法是文件未经处理就被存储了:)

标签: python django database csv


【解决方案1】:

我必须为我的一个项目这样做。我这样做的方法是创建两个模型。一个定义学生,另一个定义要导入的文件。然后,如果我使用批量导入 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()

【讨论】:

    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多