Insert into sqlalchelmy date field

The User has enters the date into an input.

print form.date.data
>>> '2017-03-05'

now i want to enter the the date or into this

expiration_date = db.Column(db.DateTime)

the error i get is

sqlalchemy.exc.StatementError



  StatementError: <unprintable StatementError object>


raise TypeError("SQLite DateTime type only accepts Python "

What can i do to get rid of convert it to the right format or fix the error.

Answer

form.date.data is a string it should be converted to python datetime because sqlalchemy DateTime column is a python datetime. Or you can store date as a string. To convert it:

import datetime
y, m, d = form.date.data.split('-')
expiration_date = datetime.datetime(int(y), int(m), int(d))

Leave a Reply

Your email address will not be published. Required fields are marked *