0
votes

I try to populate SQLAlchemy model from dictionary. The code part is like that.

data = dict(request.form)
c=customer(**data)
db.session.add(c)
db.session.commit() 

All is fine when form does not have date field. However when add date time field to form, it is not possible to fill model. I get this error message.

SQLite DateTime type only accepts Python datetime and date objects as input. [SQL: 'INSERT INTO customer (first_name, last_name, register_date) VALUES (?, ?, ?)'] [parameters: [{'first_name': ['jhon'], 'register_date': ['2010-01-01'], 'last_name': ['snow']}]]

class customer(db.Model):
first_name = db.Column(db.String(200))
last_name= db.Column(db.String(200))
register_date=db.Column(db.DateTime)

How can I fix it?

1
Sqlite doesn't have a "datetime" type, so this is something specific to the python library you're using. What does its documentation say about storing dates and times? - Shawn

1 Answers

0
votes

It looks like register_date is a string in your code. You need to convert the string to a datetime or date object before calling c=customer(**data).