我正在一个django项目中上传csv,但它显示了从笔记本电脑到笔记本电脑的错误。
模型.py

    date = models.DateTimeField(blank=True, null=True, default=datetime.date.today)

视图.py
csv_file = request.FILES['file']
data_set = csv_file.read().decode('UTF-8')
io_string = io.StringIO(data_set)
next(io_string)

uploaded_by = request.user

for column in csv.reader(io_string, delimiter=',', quotechar='|'):
    _, created = ItemBatch.objects.update_or_create(name=column[0], pid=column[1], quantity=column[2],date=column[8])

问题是它只采用这种格式:
YYYY-MM-DD HH:MM

我用这个更新了settings.py:
DATETIME_INPUT_FORMATS = [
'%Y-%m-%d %H:%M:%S',
'%d-%m-%Y %H:%M:%S',
'%Y-%m-%d %H:%M:%S.%f',
'%Y-%m-%d %H:%M',
'%Y-%m-%d',
'%m/%d/%Y %H:%M:%S',
'%m/%d/%Y %H:%M:%S.%f',
'%m/%d/%Y %H:%M',
'%m/%d/%Y',
'%m/%d/%y %H:%M:%S',
'%m/%d/%y %H:%M:%S.%f',
'%m/%d/%y %H:%M',
'%m/%d/%y',
'%d-%m-%Y %H:%M'
]




LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = False

USE_TZ = False

错误:
[“'10-7-2019 12:00'值的格式无效。一定在里面
YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]格式。“]
我需要做什么更改才能使它接受每个日期时间格式?
编辑
根据rudraa的建议,我在CustomManager函数中定义了我的格式:
def get_date_value(self, value):
    FORMATS = [
        '%d-%m-%y %H:%M',
        '%d-%y-%m %H:%M',
        '%m-%d-%y %H:%M',
        '%m-%y-%d %H:%M',
        '%y-%m-%d %H:%M',
        '%y-%d-%m %H:%M',
        '%d/%m/%y %H:%M',
        '%d/%y/%m %H:%M',
        '%m/%d/%y %H:%M',
        '%m/%y/%d %H:%M',
        '%y/%m/%d %H:%M',
        '%y/%d/%m %H:%M'
    ]
    input_formats = FORMATS

但是,当上传15/7/2019 18:30格式的文件时,即%d/%m/%Y%H:%m,它会显示:
将组名“d”重新定义为组6;组1位于位置133

最佳答案

据我所见,DATETIME_INPUT_FORMATS只在django forms中使用。它将验证以DATETIME_INPUT_FORMATS格式给出的任何日期。但是,当您直接使用模型来存储数据时,它只接受YYYY-MM-DD HH:MM中给定的数据。它根据这个regex计算datetime字符串。因此,我建议编写一个custom manager来将值转换为datetime格式,并使用它来创建/更新值。例如:

from django.utils import formats


class CustomManager(models.Manager):
    def custom_update_or_create(self,*args, **kwargs):
        date_time_value = kwargs.pop('date', None)
        if date_time_value:
            kwargs['date'] = self.get_date_value(date_time_value)
        return super(CustomManager, self).update_or_create(*args, **kwargs)

    def get_date_value(self, value):
         input_formats = [
            '%d-%m-%y %H:%M',
            '%d-%y-%m %H:%M',
            '%m-%d-%y %H:%M',
            '%m-%y-%d %H:%M',
            '%y-%m-%d %H:%M',
            '%y-%d-%m %H:%M',
            '%d/%m/%y %H:%M',
            '%d/%y/%m %H:%M',
            '%m/%d/%y %H:%M',
            '%m/%y/%d %H:%M',
            '%y/%m/%d %H:%M',
            '%y/%d/%m %H:%M'
         ]

         for format in input_formats:
            try:
                return self.strptime(value, format)
            except (ValueError, TypeError):
                continue

    def strptime(self, value, format):
        return datetime.datetime.strptime(value, format)

并在模型中使用:
class ItemBatch(models.Model):
    date = models.DateTimeField(blank=True, null=True, default=datetime.date.today)
    # rest of the fields
    objects = CustomManager()

最后,使用该方法创建/更新新实例:
ItemBatch.objects.custom_update_or_create(name=column[0], pid=column[1], quantity=column[2],date=column[8])

关于python - 在Django中从CSV上传任何DateTime,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57005777/

10-14 18:10