Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

SQLALchemy and Python - Getting the SQL result

I am using cloudkitty which is rating module in OpenStacks.

But here question is regarding the SQLAlchemy and Python.

I am new to SQLAlchemy.

I need to fetch some details from a table using a API call.

So I am creating a API call which is as follows:

curl -H "X-Auth-Token: token" http://128.0.0.1:8888/v1/report/invoice?invoice_id=7b73b9644e8242b3a740afc4659d9829

Which returned the data as follows:

"[<cloudkitty.storage.sqlalchemy.models.InvoiceDetails object at 0x7ff8b1dfe2d0>]"

Code which is returning the above result is as follows:

def get_invoice(self, tenant_id=None, invoice_id=None):

model = models.InvoiceDetails

session = db.get_session()
q = session.query(model)

if tenant_id:
        q = q.filter(model.tenant_id == tenant_id)
if invoice_id:
        q = q.filter(model.invoice_id == invoice_id)

print q
r = q.all()

return r

Need to find the way for Fetching the value instead of object.

Any help is appreciated.

Need to return it as values instead of object.

Note:

When I was trying to print the Query and executing the same in back-end it returns results correctly.(print q)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

r = q.all() returns raw SQLAlchemy results (iterable of instances). Python and SQLAlchemy do not have information how to serialize this for HTTP presentation. It simply calls Python string representation __str__ function which gives the default Python debug output.

What you most likely want to have is JSON presentation, as a common web de facto standard and platform neutral format, of SQLAlchemy data. It is extensively covered in this question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...