I am uploading CSVs in a django project but it shows error from laptop to laptop.
Models.py
date = models.DateTimeField(blank=True, null=True, default=datetime.date.today)
views.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])
The problem is that it takes only this format :
YYYY-MM-DD HH:MM
I updated settings.py with this:
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
error:
["'10-7-2019 12:00' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
What changes do I need to make such that it accepts every datetime format?
EDIT as per rudraa's suggestion I defined my formats in the CustomManager function:
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
But while uploading the file with 15/7/2019 18:30 format i.e '%d/%m/%Y %H:%M' it says:
redefinition of group name 'd' as group 6; was group 1 at position 133