0

I'm creating a blog in Flask but I'm getting an issue while creating blog posts. I always get this error:

SQLite DateTime type only accepts Python datetime and date objects as input.

It's showing the error as coming from my models.py file.

Here is my models.py file:

class Post(db.Model):
    __tablename__ = 'posts'
    id = db.Column(db.Integer,primary_key = True)
    title = db.Column(db.String(50),)
    time = db.Column(db.DateTime,nullable=False,default=str(datetime.datetime.now().strftime("%a, %b %d, %Y")))
    description = db.Column(db.Text,)
    user_id = db.Column(db.Integer,db.ForeignKey('users.id'))

    def __init__(self,title,description,user_id):
        self.title = title
        self.description = description
        self.user_id = user_id
1
  • You try to parse a string into a Datetime object. Create a real datetime object, example: datetime(2012, 3, 3, 10, 10, 10). Duplicate Commented Mar 3, 2020 at 22:15

2 Answers 2

1

The datetime object should not be converted to a string. It should be:

time = db.Column(db.DateTime,nullable=False,default=(datetime.datetime.now))
Sign up to request clarification or add additional context in comments.

1 Comment

actually you should not pass the function result, but rather the function reference: default=datetime.datetime.now
0

Use Integer type rather than DateTime in the model column declaration.

time = db.Column(db.Integer,nullable=False,default=(datetime.datetime.now))

because there is not DateTime format in SQLite

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.