0

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?

fewlinesofcode
  • 3,007
  • 1
  • 13
  • 30
  • 1
    Prolly related: https://stackoverflow.com/questions/30344237/error-sqlite-datetime-type-only-accepts-python-datetime-and-date-objects-a, https://stackoverflow.com/questions/44607244/sqlite-date-type-only-accepts-python-date-objects-as-input, https://stackoverflow.com/questions/40536368/python-datatime-object-issue-with-sqlalchemy, https://stackoverflow.com/questions/15644808/insertmany-into-the-in-memory-sqllite-db-sqlite-date-type-only-accepts-python, https://stackoverflow.com/questions/23975170/statementerror-sqlite-date-type-only-accepts-python-date-objects-as-input. Please provide a [mcve] – Ilja Everilä Nov 05 '18 at 16:46
  • 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 Nov 05 '18 at 18:57

1 Answers1

0

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).

J. Owens
  • 832
  • 7
  • 9
  • register_date is form field and it is a input as other fields. There is not problem when set register_date as c.register_date=form.register_date.data. – Saygın Ateş Nov 07 '18 at 09:47
  • What's the type of `data['register_date']` in your original post and what's the type of `form.register_date.data`? – J. Owens Nov 07 '18 at 16:36