python - CSV to Django models Date format in Django Models -
in models.py
from_date = models.datefield(auto_now=false,auto_now_add=false) to_date = models.datefield(auto_now=false,auto_now_add=false)
my csv has
in views.py im inserting data csv
reader = csv.dictreader(csvf) row in reader: csv.objects.create(from_date=row['from'],to_date=row['to'])
django throwing me error,"the date should must in yyyy-mm-dd format." there way change date format in django models directly out django model forms ?
the error message clear, should change eg: 01/05/17
2017-05-01
def format_date(date): date = date.split('/')[0] month = date.split('/')[1] year = date.split('/')[2] return '20%s-%s-%s' % (year, month, date) # eg: '2017-05-01' reader = csv.dictreader(csvf) row in reader: _from = format_date(row['from']) _to = format_date(row['to']) csv.objects.create(from_date=_from, to_date=_to)
Comments
Post a Comment