我有这样的db.model:
class UserProfile(db.Model):
__tablename__ = 'UserProfile'
nickname = db.Column(db.String(40), primary_key=True)
wm = db.Column(db.Boolean)
def __init__(self,name):
self.nickname = name
self.wm = 1
def __repr__(self):
return '<UserProfile {nickname}>'.format(username=self.nickname)在用户登录期间-我试图从db检索记录,并将其值存储在会话变量中-
userprofile = UserProfile(form.username.data)
userprofile = UserProfile.query.filter_by(nickname=form.username.data).first()
session['wm']=userprofile.wm但它失败了,并显示如下消息:
session['wm']=userprofile.wm
AttributeError: 'NoneType' object has no attribute 'wm'Mysql数据库:
mysql> desc UserProfile;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| nickname | varchar(40) | NO | PRI | NULL | |
| wm | tinyint(1) | YES | | NULL | |它也有记录。
谢谢你的帮助。
发布于 2013-03-24 22:20:08
您需要首先将新的UserProfile对象添加到数据库中:
userprofile = UserProfile(form.username.data)
db.session.add(userprofile)请参阅Flask-SQLAlchemy documentation on insertion
在您将对象添加到会话之前,SQLAlchemy基本上不打算将其添加到事务中。这很好,因为您仍然可以放弃更改。例如,考虑在页面上创建帖子,但您只想将帖子传递给模板以进行预览呈现,而不是将其存储在数据库中。
然后,add()函数调用添加对象。
发布于 2015-08-13 19:33:27
添加后,您需要提交,以查看更改
userprofile = UserProfile(form.username.data)
db.session.add(userprofile)
db.session.commit()https://stackoverflow.com/questions/15599519
复制相似问题