继承问题:AttributeError:'str'对象没有属性'_sa_instance_state'

时间:2015-10-13 21:19:03

标签: python attributes sqlalchemy traceback

问题:继承类没有检查字符串,因为它是作为None传递的。

检索方法的父类( object.py )旨在从数据库中检索对象(例如id或user / assessment组合)。子类( convenience.py )旨在接受这些对象的str个。

object.py

def retrieve_assessment_result(self, *args):
    id, owner, assessment = None, None, None
    if len(args) == 1:
        id, = args # or id = args[0]
    elif len(args) == 2:
        owner, assessment = args
        print 'testa', owner, assessment, id
    else:
        raise ValueError('Value being passed is an object')
    if id is not None:
        print 'testi', id
        return self.session.query(Assessment_Result).\
        filter(Assessment_Result.id == id).one()
    elif owner is not None:
        print 'testo', owner
        return self.session.query(Assessment_Result).\
        filter(Assessment_Result.owner == owner).one()
    elif assessment is not None:
        print 'testa', assessment
        return self.session.query(Assessment_Result).\
        filter(Assessment_Result.assessment == assessment).one()

convenience.py

def retrieve_assessment_result(self, *args):
    id, owner, assessment = None, None, None
    if len(args) == 1:
        id, = args # or id = args[0]
        print " args = ", args
    elif len(args) == 2:
        username, name = args
        print "api args = ", args
        print 'type args', type(args)
        print 'owner: ', owner
        print 'assessment: ', assessment
    if owner is not None:
        owner = self.retrieve_user(username)
    if assessment is not None:
        assessment = self.retrieve_assessment(name)
    return super(ConvenienceAPI, self).retrieve_assessment_result(*args)

test.py

api.retrieve_assessment_result('baseball', 'Becoming a Leader')

追踪错误:

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/venv/NotssDB/notssdb/test/test.py", line 133, in test1
    api.retrieve_assessment_result('baseball', 'Becoming a Leader')
  File "/Users/ack/code/venv/NotssDB/notssdb/api/convenience.py", line 42, in retrieve_assessment_result
    return super(ConvenienceAPI, self).retrieve_assessment_result(*args)
  File "/Users/ack/code/venv/NotssDB/notssdb/api/object.py", line 327, in retrieve_assessment_result
    filter(Assessment_Result.owner == owner).one()
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/sql/operators.py", line 301, in __eq__
    return self.operate(eq, other)
  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/relationships.py", line 1011, in __eq__
    other, adapt_source=self.adapter))
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1338, in _optimized_compare
    state = attributes.instance_state(state)
AttributeError: 'str' object has no attribute '_sa_instance_state'
------output--------
<Assessment_Result(owner='<User(username ='bambam', firstname ='Sean', lastname ='Cargo', email='car@gmail.com')>', assessment='<Assessment(name='Foo', text='this is foo')>')>
api args =  ('baseball', 'Becoming a Leader')
type args <type 'tuple'>
owner:  None
assessment:  None
testa baseball Becoming a Leader None
testo baseball

-----评论/建议后更新:
object.py 已更新:

def retrieve_assessment_result(self, id=None, owner=None, assessment=None):
    print 'test_all_objects', owner, assessment, id
    if id is not None:
        print 'test_id_object', id
        return self.session.query(Assessment_Result).\
        filter(Assessment_Result.id == id).one()
    elif owner is not None:
        print 'test_owner_object', owner
        return self.session.query(Assessment_Result).\
        filter(Assessment_Result.owner.has(User.username == username)).one()
    elif assessment is not None:
        print 'test_assessment_object', assessment
        return self.session.query(Assessment_Result).\
        filter(Assessment_Result.assessment.has(Assessment.name == name)).one()

Convenience.py (继承自 object.py )更改:

def retrieve_assessment_result(self, id=None, owner=username, assessment=name):
    if owner is not None:
        owner = self.retrieve_user(username)
    if assessment is not None:
        assessment = self.retrieve_assessment(name)
    return super(ConvenienceAPI, self).retrieve_assessment_result(id=None, owner=owner, assessment=assessment)

追溯:

  File ".../api/convenience.py", line 6, in <module>
    class ConvenienceAPI(BaseAPI):
  File "../api/convenience.py", line 37, in ConvenienceAPI
    def retrieve_assessment_result(self, id=None, owner=username, assessment=name):
NameError: name 'username' is not defined

1 个答案:

答案 0 :(得分:3)

根据我的经验,错误'???' object has no attribute '_sa_instance_state几乎总是表明您分配的内容不是SQLAlchemy对象(即一些基本类型,如int,str等) relationshipdynamic_loader

relationship示例

>>> result = db.query(Assessment_Result).first()
>>> # owner only accepts Owner objects
>>> result.owner = "Mr. Harvey Example"
Traceback (most recent call last):
...
AttributeError: 'str' object has no attribute '_sa_instance_state'

此外,使用relationship上的其他类型进行过滤也会导致此错误:

过滤示例

>>> Assessment_Result.owner == "baseball"
Traceback (most recent call last):
...
AttributeError: 'str' object has no attribute '_sa_instance_state'

在您的情况下,错误是由于将字符串"baseball"作为您传递给args的2元组Object.retrieve_assessment_result中的第一个条目引起的,而owner又会被放入在第二个if块中的变量>>> owner, assessment = args 中。

owner

由于str现在包含{{1}},当您使用它进行过滤时,这将失败。

我想借此机会指出此代码不易读取,可以通过使用 keyword arguments 进行改进,并且通常会使其更简洁。