Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error for AttributeError: 'KeyedTuple' object has no attribute 'json'

I am new to Python and exploring SQLAlchemy. Here is my code

class Test(Base):
     __tablename__ = 'test'
     __public__ = ('my_id', 'name')
     my_id = Column(Integer, primary_key=True)
     name = Column(String)
     def __init__(self, id, name):
         self.my_id = id
         self.name = name
     def __repr__(self):
        return "<User('%d','%s')>" % (self.id, self.name)
     @property   
     def json(self):    
        return to_json(self, self.__class__)

users= cess.query(Test.my_id, Test.name).order_by(Test.my_id).distinct().all()
for c in users:
    print c.json

But i am getting an error AttributeError: 'KeyedTuple' object has no attribute 'json' what am i doing wrong.

Kind Regards

like image 547
AndroidDev Avatar asked Oct 01 '13 18:10

AndroidDev


1 Answers

You query is

cess.query(Test.my_id, Test.name)

so you don't get Test instances as a result. It means you don't have json property on your results. Instead you get KeyedTuples that are tuples with attributes you requested, i.e. my_id and name. To make it work you can change your query to

cess.query(Test)
like image 200
zaquest Avatar answered Nov 14 '22 22:11

zaquest