数据库存储使用datetime.utcnonw()flask-sqlalchemy和postgresql

时间:2017-09-19 04:52:03

标签: python flask flask-sqlalchemy postgresql-9.3

我在保存评论时尝试将当前时间保存在我的数据库中,但每次将时间存储为1990.当我在python shell上运行datetime.utcnonw()时,它会返回正确的日期。我做错了什么?

这是我的模型的代码

class Review(db.Model):

    __tablename__ = 'reviews'

    dates = datetime.now()
    id = db.Column(db.Integer,primary_key = True)
    movie_id = db.Column(db.Integer)
    movie_title = db.Column(db.String)
    image_path = db.Column(db.String)
    movie_review = db.Column(db.String)
    posted = db.Column(db.DateTime(),default=datetime.utcnow)
    user_id = db.Column(db.Integer,db.ForeignKey("users.id"))

以下是将其插入数据库的代码

@main.route('/movie/review/new/<int:id>', methods = ['GET','POST'])
@login_required
def new_review(id):

    form = ReviewForm() #wtf form class that has only two fields
    movie = get_movie(id) # Function that calls an api and returns a movie

    if form.validate_on_submit():
        title = form.title.data
        review = form.review.data

        new_review = Review(movie_id=movie.id,movie_title=title,image_path=movie.poster,movie_review=review,user=current_user)
    new_review.save_review()

    return redirect(url_for('.movie',id = movie.id ))

0 个答案:

没有答案