Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 将csv上载到数据库在空单元格上失败_Django_Django Models_Django Views - Fatal编程技术网

Django 将csv上载到数据库在空单元格上失败

Django 将csv上载到数据库在空单元格上失败,django,django-models,django-views,Django,Django Models,Django Views,Django 2.0 PostgreSQL 10 重要的是,我的应用程序允许用户批量上传数据,为此他们可以上传csv文件。但是,对于任何非字符串字段,如果csv文件上的单元格为空,则返回以下错误: [““”值的日期格式无效。该格式必须为YYYY-MM-DD 格式。“] 此异常是在日期列下的空单元格中复制的,但int和float的情况相同 例外情况很简单。但是根据, 值None被写入空字符串 尽管这在csv.writer部分而不是csv.reader部分中有说明 我很难理解为什么ORM没有将空字符

Django 2.0

PostgreSQL 10

重要的是,我的应用程序允许用户批量上传数据,为此他们可以上传csv文件。但是,对于任何非字符串字段,如果csv文件上的单元格为空,则返回以下错误:

[““”值的日期格式无效。该格式必须为YYYY-MM-DD 格式。“]

此异常是在日期列下的空单元格中复制的,但int和float的情况相同

例外情况很简单。但是根据,

值None被写入空字符串

尽管这在csv.writer部分而不是csv.reader部分中有说明

我很难理解为什么ORM没有将空字符串转换为数据库能够理解的None或Null值,正如我的models.py中所述,它接受这些值

型号。py:

class Control(models.Model):
    date  = models.DateField(default=date.today, null=True, blank=True)
    kilos = models.FloatField(null=True, blank=True)
class ControlFileUpload(View):
    def get(self, request):
        return render(request, 'csv_upload.html')

    def post(self, request):
        file = request.FILES['csv_file']
        _control_bulk_data_upload(file) #imported from utils.py
        return render(request, 'csv_upload_success.html')
import csv, datetime
from app.models import Control

def _control_bulk_data_upload(csv_file):
    path = default_storage.save("{}_{}".format(datetime.utcnow(), csv_file.name), ContentFile(csv_file.read()))
    with open(path) as csvfile:
        reader = csv.reader(csvfile)
        next(reader) #skip headers
        for row in reader:
            Control.objects.create(
                date      = row[0],
                kilos     = row[1],
        )
视图。py:

class Control(models.Model):
    date  = models.DateField(default=date.today, null=True, blank=True)
    kilos = models.FloatField(null=True, blank=True)
class ControlFileUpload(View):
    def get(self, request):
        return render(request, 'csv_upload.html')

    def post(self, request):
        file = request.FILES['csv_file']
        _control_bulk_data_upload(file) #imported from utils.py
        return render(request, 'csv_upload_success.html')
import csv, datetime
from app.models import Control

def _control_bulk_data_upload(csv_file):
    path = default_storage.save("{}_{}".format(datetime.utcnow(), csv_file.name), ContentFile(csv_file.read()))
    with open(path) as csvfile:
        reader = csv.reader(csvfile)
        next(reader) #skip headers
        for row in reader:
            Control.objects.create(
                date      = row[0],
                kilos     = row[1],
        )
utils.\u控制\u批量\u数据\u上传:

class Control(models.Model):
    date  = models.DateField(default=date.today, null=True, blank=True)
    kilos = models.FloatField(null=True, blank=True)
class ControlFileUpload(View):
    def get(self, request):
        return render(request, 'csv_upload.html')

    def post(self, request):
        file = request.FILES['csv_file']
        _control_bulk_data_upload(file) #imported from utils.py
        return render(request, 'csv_upload_success.html')
import csv, datetime
from app.models import Control

def _control_bulk_data_upload(csv_file):
    path = default_storage.save("{}_{}".format(datetime.utcnow(), csv_file.name), ContentFile(csv_file.read()))
    with open(path) as csvfile:
        reader = csv.reader(csvfile)
        next(reader) #skip headers
        for row in reader:
            Control.objects.create(
                date      = row[0],
                kilos     = row[1],
        )

在写入数据库之前,只需检查该值是否为None

date = row[0] if row[0] else datetime.datetime.today()
和用于在一个查询中插入数据,而不是在循环中插入查询。这样效率更高

temp_data = []
with open(path) as csvfile:
    reader = csv.reader(csvfile)
    next(reader) #skip headers
    for row in reader:
        csv_data = Control()
        csv_data.date = row[0] if row[0] else datetime.datetime.today()
        csv_data.kilos = row[1] if row[1] else 0
        temp_data.append(csv_data)

if len(temp_data) > 0:
    bulk_create = Control.objects.bulk_create(temp_data)

在写入数据库之前,只需检查该值是否为None

date = row[0] if row[0] else datetime.datetime.today()
和用于在一个查询中插入数据,而不是在循环中插入查询。这样效率更高

temp_data = []
with open(path) as csvfile:
    reader = csv.reader(csvfile)
    next(reader) #skip headers
    for row in reader:
        csv_data = Control()
        csv_data.date = row[0] if row[0] else datetime.datetime.today()
        csv_data.kilos = row[1] if row[1] else 0
        temp_data.append(csv_data)

if len(temp_data) > 0:
    bulk_create = Control.objects.bulk_create(temp_data)

但我不想分配datetime。今天()或0,我希望字段存储为Null,然后将else更改为None。但我不想分配datetime。今天()或0,我希望字段存储为Null,然后将else更改为None。