我在嘗試將布爾語句從默認False
更新爲True
。這裏的想法是用戶觀看一些視頻,並且更新方法從False
更改爲True
。在SQL鍊金術中更新布爾值(使用CRUD)
我正在努力編寫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'
我不知道你爲什麼要'追加'到'update_view.completed'。這是一個布爾值,而不是一個列表,對嗎? – coralvanda
@coralv你如何更新布爾值? – thesayhey