#StackBounty: #python #django #datetime Upload any DateTime from CSV in Django

Bounty: 50

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?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.