SQLALchemy and Python - Getting the SQL result

2024/11/18 20:19:53

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.InvoiceDetailssession = 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)

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.

https://en.xdnf.cn/q/120031.html

Related Q&A

ValueError: invalid literal for int() with base 10: python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Closed 10 years ago.Questions asking for code must demonstrate a minimal understanding of the proble…

python code to connect to sftp server

I found this code to connect to remote sftp server with the help of username ,password and host but i also need to include the port number, can any one let em know how to include the port number in thi…

Python: get the max value with the location above and below than the max

If I have a dataframe like this, index User Value location1 1 1.0 4.5 2 1 1.5 5.23 1 3.0 7.04 1 2.5 7.55 2 1.0 11.56 2 1.…

Retrieve smart cards PAN with Python and pyscard

Im trying to retrieve the PAN of a smart card using pyscard in Python. What I have done so far is to connect to the reader and to retrieve various information about the reader and the card... but I can…

How to stop a specific key from working in Python

My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didnt work, I tried programming a code that can stop it but i couldnt because i am a beginner…

How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

trying to write a Python function: def compare_lengths(x, y, z) which takes as arguments three arrays and checks their lengths and returns them as a triple in order of length. For example, if the funct…

How to split a string in Python by 2 or 3, etc [duplicate]

This question already has answers here:Split string every nth character(21 answers)How to iterate over a list in chunks(40 answers)Closed 10 years ago.Does anyone know if its possible in python to spli…

.LAS into a .CSV file using python

How to change a .las file into a .csv file? Have been trying myself but no luck no far. I am just looking for something decently short that will save some time when I have to convert big .olas files i…

using pandas read_excel to read from stdin

Note: I have solve this problem as per below:I can use to_csv to write to stdout in python / pandas. Something like this works fine:final_df.to_csv(sys.stdout, index=False)I would like to read in an a…

How to print a string x times based on user input [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Understanding for loops in Python(4 answers)Closed 1 year ago.I am trying to make a simple application that will pri…