2016-11-14 34 views
1

我在嘗試將布爾語句從默認False更新爲True。這裏的想法是用戶觀看一些視頻,並且更新方法從False更改爲True在SQL鍊金術中更新布爾值(使用CRUD)

我正在努力編寫update_view_state方法。我嘗試了點符號(我承認這是新的)並且不成功。

使用Python 2.7SQLALCHEMYCRUD 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' 
+0

我不知道你爲什麼要'追加'到'update_view.completed'。這是一個布爾值,而不是一個列表,對嗎? – coralvanda

+0

@coralv你如何更新布爾值? – thesayhey

回答

1

在回答您的意見,我覺得這是更好,我只是張貼解答於提供細節。希望這可以幫助你清理並正常運行。

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(),但我試圖與您發佈的內容保持一致,因爲可能需要某些其他未發佈的部分的代碼。

我已經直接更新到布爾True這裏,但我看到你已經採用了你可能更喜歡使用的功能參數completed。在這種情況下,只需使用update_view.completed = completed即可。也許考慮重命名該參數以避免與數據庫中具有相同名稱的列混淆。

至於你問,如果我們的參數重新命名completedstatus_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要麼TrueFalse如果你想來回切換。這樣做只是將所選布爾值作爲參數傳遞給函數。如果您只想使用此功能將其設置爲True,那麼您可以完全刪除status_change參數,並像第一個示例中那樣使用原始布爾值。

+0

如果你要重命名它,說'status_change'作爲變量以避免混淆,那麼在上面的代碼中看起來如何? – thesayhey

+0

我正在追溯。查看高於^ – thesayhey

+1

發現此問題。由於我正在使用用戶和視頻對象,因此我需要爲查詢進行連接。這個答案現在適用!我將添加上面的工作查詢。 – thesayhey