Flask DateTime不会设置为默认的datetime.utcnow [英] Flask DateTime won't set with default datetime.utcnow

查看:217
本文介绍了Flask DateTime不会设置为默认的datetime.utcnow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SQLAlchemy的新手,无法设置创建的DateTime.我尝试使用在许多示例中找到的默认"选项.我也尝试过手动设置(已将其注释掉).到目前为止都没有工作.任何帮助将不胜感激.

I am new to SQLAlchemy and have been unable to set the DateTime for created. I have tried using the "default" option found in many examples. I have also tried setting it manually (I have it commented out). Neither have worked so far. Any help would be appreciated.

models.py

import datetime
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash

db = SQLAlchemy()

class User(db.Model):

  __tablename__    = 'users'
  uid              = db.Column(db.Integer, primary_key=True)
  firstname        = db.Column(db.String(40))
  lastname         = db.Column(db.String(40))
  email            = db.Column(db.String(120), unique=True)
  created          = db.Column(db.DateTime, default=datetime.datetime.utcnow())
  confirmed        = db.Column(db.DateTime, nullable=True)
  pwdhash          = db.Column(db.String(100))

  def __init__(self, firstname, lastname, email, password):
    self.firstname = firstname.title()
    self.lastname  = lastname.title()
    self.email     = email.lower()
    self.set_password(password)
    #self.created   = datetime.datetime.utcnow()
    self.confirmed = None

  def set_password(self, password):
    self.pwdhash   = generate_password_hash(password)

  def check_password(self, password):
    return check_password_hash(self.pwdhash, password)

routes.py

from tasks import app
from datetime import datetime
from flask import render_template, request, flash, session, url_for, redirect
from forms import ContactForm, SignupForm, SigninForm
from flask.ext.mail import Message, Mail
from models import db, User, Tags, Tasks

@app.route('/signup', methods=['GET', 'POST'])
def signup():
  form = SignupForm()

  if 'email' in session:
    return redirect(url_for('profile')) 

  if request.method == 'POST':
    if form.validate() == False:
      return render_template('signup.html', form=form)
    else:
      newuser = User(form.firstname.data, form.lastname.data, form.email.data, form.password.data)
      db.session.add(newuser)
      db.session.commit()

      session['email'] = newuser.email
      return redirect(url_for('profile'))

  elif request.method == 'GET':
    return render_template('signup.html', form=form)

推荐答案

default的问题是您立即在那里调用datetime.utcnow,并且返回的值(在类定义时)始终为用作默认值.您需要像下面这样传递可调用对象本身:

The problem with your default is that you're calling datetime.utcnow immediately there, and the value returned (at the time of class definition) is always used as default. You need to pass the callable itself like following:

# Note the lack of parenthesis after datetime.utcnow
created = db.Column(db.DateTime, default=datetime.datetime.utcnow)

这样,SQLAlchemy将在插入行时调用datetime.utcnow本身.

This way SQLAlchemy will call datetime.utcnow itself upon row insert.

这篇关于Flask DateTime不会设置为默认的datetime.utcnow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆