Python 2.7 更新SQL Alchemy中的布尔值(使用CRUD)

Python 2.7 更新SQL Alchemy中的布尔值(使用CRUD),python-2.7,sqlalchemy,boolean,crud,traceback,Python 2.7,Sqlalchemy,Boolean,Crud,Traceback,我正在尝试将布尔语句从默认的False更新为True。这里的想法是,用户观看一些视频,更新方法从False更改为True 我正在努力编写update\u view\u state方法。我尝试过点表示法(我承认是新手),但没有成功 使用python2.7SQLALCHEMY和CRUD方法 class View_State(Base): __tablename__ = 'view_states' #more code completed = Column(Boolean, d

我正在尝试将布尔语句从默认的
False
更新为
True
。这里的想法是,用户观看一些视频,更新方法从
False
更改为
True

我正在努力编写
update\u view\u state
方法。我尝试过点表示法(我承认是新手),但没有成功

使用
python2.7
SQLALCHEMY
CRUD方法

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'

下面给出答案:

回溯:

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'

作为对你评论的回应,我认为我最好只发布一个答案来提供细节。希望这能帮助你把它清理干净并正常运行

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
,但是我看到您已经使用了一个您可能更喜欢使用的函数paremeter
completed
。在这种情况下,只需使用
update\u view.completed=completed
。也许考虑重命名该参数以避免与数据库中同名的列混淆。

如您所问,如果我们将参数
completed
重命名为
status\u 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\u view.completed
设置为
True
False
。这样做只是将您选择的布尔值作为参数传递给函数。如果您只想使用此函数将其设置为
True
,则可以完全删除
status\u change
参数,并使用第一个示例中的原始布尔值。

我不确定您为什么要尝试将
附加到
update\u视图。已完成
。这是一个布尔值,不是一个列表,对吗?@coralv如何更新布尔值?如果要重命名它,比如说
status\u change
作为变量以避免混淆,那么上面的代码中会是什么样子?我正在进行回溯。请参见上文^发现问题。因为我使用的是用户和视频对象,所以我需要对查询进行连接。这个答案现在适用了!我将在上面添加工作查询。很高兴您发现了问题并使其工作!我将编辑我的答案,将
completed
更改为
status\u change
,这样您就可以看到它是什么样子了。