检索方法问题? AttributeError抱怨'str'对象

时间:2015-10-02 16:32:46

标签: python sqlalchemy traceback

问题:AttributeError: 'str' object has no attribute '_sa_instance_state'我不确定如何修复它。 我相信它与下面的retrieve方法有关。 Stacks上的某个人有类似的问题,这是一个改变功能的简单问题......对我来说情况并非如此。

追溯:

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 132, in test1
    api.retrieve_assessment_result('baseball', 'Becoming a Leader')
  File "/Users/ack/code/venv/NotssDB/notssdb/api/object.py", line 324, 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'

object.py

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

def create_category_rating(self, category_rating_int, category, assessment_result):
    new_catrating = Category_Rating(category_rating_int, category, assessment_result)
    self.session.add(new_catrating)
    print(new_catrating)
    self.session.commit()
    return(new_catrating)

convenience.py (继承自object.py)

def create_category_rating(self, category_rating_int, category_name, username, name):
    category = self.retrieve_category(category_name)
    owner = self.retrieve_user(username)  
    assessment = self.retrieve_assessment(name)  
    assessment_result = self.retrieve_assessment_result(owner, assessment)
    return super(ConvenienceAPI, self).create_category_rating(category_rating_int, category, assessment_result)

model.py

class Category_Rating(Base):
    __tablename__ = 'category_ratings'

    id = Column(Integer, primary_key=True)
    category_rating_int = Column(Integer)

    category_id = Column(Integer, ForeignKey('categories.category_id'))
    category = relationship('Category', backref='category_ratings')

    assessment_result_id = Column(Integer, ForeignKey('assessment_results.id'))
    assessment_result = relationship('Assessment_Result', backref='category_ratings')

    def __init__(self, category_rating_int, category, assessment_result): #OBJECT
        self.category_rating_int = category_rating_int
        self.category = category
        self.assessment_result  = assessment_result

    def __repr__(self):
        return "<Category_Rating(category_rating_int='%s')>" % (self.category_rating_int)

test.py

api = ConvenienceAPI()
api.create_category_rating(2, 'Decision-Making', 'baseball', 'Becoming a Leader')

2 个答案:

答案 0 :(得分:2)

您的问题与链接问题中的问题完全相同:

  

您正在为uselist=False的关系分配一个列表。您应该设置单个模型实例,而不是包含它的列表。

在您的情况下,属性assessment_results被定义为多对一关系(例如,单个Category_Rating可以包含单个Assessment_Results个实例,但单个Assessment_Results可以包含多个 Category_Rating个实例)。如果您执行以下重命名,之前的语句将对您更有意义:

assessment_results -> assessment_result
assessment_results_id -> assessment_result_id
Assessment_Results -> Assessment_Result

问题是assessment_results属性需要Assessment_Results模型的单个实例,但是您将其设置为方法retrieve_assessment_results()的结果,该方法返回Assessment_Results的列表。这就是它失败的原因。

<强> UPD

第二个问题在于此字符串filter(Assessment_Result.owner == owner).one()。您尝试将Owner实例与字符串进行比较。您可能希望将其替换为filter(Owner.name == owner).one()之类的内容。如果没有看到这些模型的定义,就无法准确地告诉你。

答案 1 :(得分:0)

与Yaroslav Admin合作,我做了类似于他的建议...我在convenience.py(api)中为assessment_result(..)添加了一个检查字符串的检索方法:

工作代码:
(这继承自object.py文件 - 如上所示)

def retrieve_assessment_result(self, username, name):
    owner = self.retrieve_user(username)
    assessment = self.retrieve_assessment(name)
    return super(ConvenienceAPI, self).retrieve_assessment_result(owner, assessment)