Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...