在 SQL Alchemy 中更新布尔值(使用 CRUD)
Update boolean in SQL Alchemy (using CRUD)
我正在尝试将布尔语句从默认值 False
更新为 True
。这里的想法是用户观看了一些视频并且更新方法从 False
更改为 True
。
我正在努力编写 update_view_state
方法。我已经尝试过点符号(我承认我是新手)但没有成功。
使用 Python 2.7
SQLALCHEMY
和 CRUD method
class View_State(Base):
__tablename__ = 'view_states'
#more code
completed = Column(Boolean, default=False) #have to set default
def __init__(self, user, video):
self.completed = False
更新代码:
def update_view_state(self, username, videoname, completed):
#update Boolean completed status to 'complete = True'
update_view = self.session.query(View_State).\
filter(View_State.user.has(username == username)).\
filter(View_State.video.has(videoname == videoname)).one()
if update_view:
print 'update view found: ', update_view.completed
update_view.completed.append(completed)
self.session.commit()
return update_view
Test.py
api.update_view_state('ack', 'module1', True)
回溯:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/test/test.py", line 296, in test_crud_operations
api.update_view_state('ack', 'module1', True)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/convenience.py", line 35, in update_view_state
return super(ConvenienceAPI, self).update_view_state(user, video, completed)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/object.py", line 634, in update_view_state
filter(View_State.user.has(username == username)).\
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 442, in has
return self.operate(PropComparator.has_op, criterion, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 175, in operate
return op(self.comparator, *other, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 383, in has_op
return a.has(b, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1172, in has
return self._criterion_exists(criterion, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1081, in _criterion_exists
criterion = criterion._annotate(
AttributeError: 'bool' object has no attribute '_annotate'
下面的答案:
回溯:
ERROR: notssdb.test.test.test_crud_operations
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/test/test.py", line 295, in test_crud_operations
api.update_view_state('ack', 'module1', True)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/object.py", line 628, in update_view_state
filter(View_State.user.has(username == username)).\
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 442, in has
return self.operate(PropComparator.has_op, criterion, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 175, in operate
return op(self.comparator, *other, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 383, in has_op
return a.has(b, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1172, in has
return self._criterion_exists(criterion, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1081, in _criterion_exists
criterion = criterion._annotate(
AttributeError: 'bool' object has no attribute '_annotate'
针对您的评论,我认为我最好 post 提供详细信息的答案。希望这可以帮助您清理它并正确 运行。
def update_view_state(self, username, videoname, completed):
#update Boolean completed status to 'complete = True'
update_view = self.session.query(View_State).\
filter(View_State.user.has(username == username)).\
filter(View_State.video.has(videoname == videoname)).one()
if update_view:
print 'update view found: ', update_view.completed
update_view.completed = True # Simply reassign it like a variable
self.session.add(update_view) # Add it to the session
self.session.commit() # And commit
return update_view
我在代码中使用了 self.session
,因为您正在使用它。我从来没有以这种方式设置我的会话,所以我只使用过 session.commit()
或 session.add()
,但我试图与你所获得的 posted 保持一致,因为您的代码的其他一些未posted 部分可能需要这样做。
我在这里直接更新了布尔值 True
,但我看到您已经采用了您可能更喜欢使用的函数参数 completed
。在这种情况下,只需使用 update_view.completed = completed
。也许考虑重命名该参数以避免与数据库中同名的列混淆。
如您所问,如果我们将参数 completed
重命名为 status_change
,它将如下所示:
def update_view_state(self, username, videoname, status_change):
#update Boolean completed status to 'complete = True'
update_view = self.session.query(View_State).\
filter(View_State.user.has(username == username)).\
filter(View_State.video.has(videoname == videoname)).one()
if update_view:
print 'update view found: ', update_view.completed
update_view.completed = status_change # Simply reassign it like a variable
self.session.add(update_view) # Add it to the session
self.session.commit() # And commit
return update_view
在这种情况下,如果您想来回切换,可以使用函数将 update_view.completed
设置为 True
或 False
。这样做只是将您选择的布尔值作为参数传递给函数。如果您只想使用此函数将其设置为 True
,则可以完全删除 status_change
参数,并使用第一个示例中的原始布尔值。
我正在尝试将布尔语句从默认值 False
更新为 True
。这里的想法是用户观看了一些视频并且更新方法从 False
更改为 True
。
我正在努力编写 update_view_state
方法。我已经尝试过点符号(我承认我是新手)但没有成功。
使用 Python 2.7
SQLALCHEMY
和 CRUD method
class View_State(Base):
__tablename__ = 'view_states'
#more code
completed = Column(Boolean, default=False) #have to set default
def __init__(self, user, video):
self.completed = False
更新代码:
def update_view_state(self, username, videoname, completed):
#update Boolean completed status to 'complete = True'
update_view = self.session.query(View_State).\
filter(View_State.user.has(username == username)).\
filter(View_State.video.has(videoname == videoname)).one()
if update_view:
print 'update view found: ', update_view.completed
update_view.completed.append(completed)
self.session.commit()
return update_view
Test.py
api.update_view_state('ack', 'module1', True)
回溯:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/test/test.py", line 296, in test_crud_operations
api.update_view_state('ack', 'module1', True)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/convenience.py", line 35, in update_view_state
return super(ConvenienceAPI, self).update_view_state(user, video, completed)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/object.py", line 634, in update_view_state
filter(View_State.user.has(username == username)).\
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 442, in has
return self.operate(PropComparator.has_op, criterion, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 175, in operate
return op(self.comparator, *other, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 383, in has_op
return a.has(b, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1172, in has
return self._criterion_exists(criterion, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1081, in _criterion_exists
criterion = criterion._annotate(
AttributeError: 'bool' object has no attribute '_annotate'
下面的答案:
回溯:
ERROR: notssdb.test.test.test_crud_operations
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/test/test.py", line 295, in test_crud_operations
api.update_view_state('ack', 'module1', True)
File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/object.py", line 628, in update_view_state
filter(View_State.user.has(username == username)).\
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 442, in has
return self.operate(PropComparator.has_op, criterion, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 175, in operate
return op(self.comparator, *other, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 383, in has_op
return a.has(b, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1172, in has
return self._criterion_exists(criterion, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1081, in _criterion_exists
criterion = criterion._annotate(
AttributeError: 'bool' object has no attribute '_annotate'
针对您的评论,我认为我最好 post 提供详细信息的答案。希望这可以帮助您清理它并正确 运行。
def update_view_state(self, username, videoname, completed):
#update Boolean completed status to 'complete = True'
update_view = self.session.query(View_State).\
filter(View_State.user.has(username == username)).\
filter(View_State.video.has(videoname == videoname)).one()
if update_view:
print 'update view found: ', update_view.completed
update_view.completed = True # Simply reassign it like a variable
self.session.add(update_view) # Add it to the session
self.session.commit() # And commit
return update_view
我在代码中使用了 self.session
,因为您正在使用它。我从来没有以这种方式设置我的会话,所以我只使用过 session.commit()
或 session.add()
,但我试图与你所获得的 posted 保持一致,因为您的代码的其他一些未posted 部分可能需要这样做。
我在这里直接更新了布尔值 True
,但我看到您已经采用了您可能更喜欢使用的函数参数 completed
。在这种情况下,只需使用 update_view.completed = completed
。也许考虑重命名该参数以避免与数据库中同名的列混淆。
如您所问,如果我们将参数 completed
重命名为 status_change
,它将如下所示:
def update_view_state(self, username, videoname, status_change):
#update Boolean completed status to 'complete = True'
update_view = self.session.query(View_State).\
filter(View_State.user.has(username == username)).\
filter(View_State.video.has(videoname == videoname)).one()
if update_view:
print 'update view found: ', update_view.completed
update_view.completed = status_change # Simply reassign it like a variable
self.session.add(update_view) # Add it to the session
self.session.commit() # And commit
return update_view
在这种情况下,如果您想来回切换,可以使用函数将 update_view.completed
设置为 True
或 False
。这样做只是将您选择的布尔值作为参数传递给函数。如果您只想使用此函数将其设置为 True
,则可以完全删除 status_change
参数,并使用第一个示例中的原始布尔值。