瓶的安全性。将datetime(2014,5,31,23,46,38,559513)转换为python格式datetime(2014-5-31,23:46:38.559513)

时间:2014-06-01 01:12:42

标签: python python-datetime flask-security

我正在使用Flask-Security,除了电子邮件确认链接外,一切正常。很明显,当用户注册时他收到一封带有确认电子邮件链接的电子邮件,因此当他/她点击该链接时,我的应用程序会给出一个SQL datetime fromat错误。确切的错误是:

sqlalchemy.exc.StatementError
StatementError: SQLite DateTime type only accepts Python datetime and date objects as input. (original cause: 
TypeError: SQLite DateTime type only accepts Python datetime and date objects as input.) 
'UPDATE user SET confirmed_at=?, last_login_at=?, current_login_at=?, last_login_ip=?, current_login_ip=?,
login_count=? WHERE user.id = ?' [{u'user_id': 1, 'confirmed_at': datetime.datetime(2014, 5, 31, 23, 46, 38, 559513),
'login_count': 1, 'last_login_ip': '127.0.0.1', 'last_login_at': datetime.datetime(2014, 5, 31, 23, 46, 38, 557310),
'current_login_at': datetime.datetime(2014, 5, 31, 23, 46, 38, 557310), 'current_login_ip': '127.0.0.1'}]

因此,想问我该怎么做才能克服这个错误。

这是我的models.py:

from app import db
from flask.ext.security import UserMixin, RoleMixin

#association table that stores users and their roles
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

#orm table that stores information on roles 
class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))


class User(db.Model, UserMixin):
    """a class that used to create ORM Database and 
    objects related to the class"""

    id = db.Column(db.Integer, primary_key = True)
    email = db.Column(db.String(255), unique = True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

    # Extra fields for Flask-Security
    name = db.Column(db.String(255))
    surname = db.Column(db.String(255))
    birth_date  = db.Column(db.Date)

    # SECURITY_CONFIRMABLE 
    confirmed_at = db.Column(db.DateTime())

    # SECURITY_TRACKABLE
    last_login_at = db.Column(db.DateTime())
    current_login_at = db.Column(db.DateTime())
    last_login_ip = db.Column(db.String(40))
    current_login_ip = db.Column(db.DateTime())
    login_count = db.Column(db.Integer(6))

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

    def set_password():
        pass

    def check_password():
        pass

    def __repr__(self):
        return '<User %r>' % (self.username)

1 个答案:

答案 0 :(得分:1)

您的current_login_ip列包含错误类型:

current_login_ip = db.Column(db.DateTime())

它需要一个日期时间,而你传入的是一个包含ip地址('127.0.0.1')的字符串。它应该可能db.String()列,例如last_login_ip

current_login_ip = db.Column(db.String(40))

其余的输入值都很好;没有datetime(2014-5-31, 23:46:38.559513)格式的东西。