How to return multiple values in a function in python?

1

I'm developing an API in Python FLASK and I need to show the result of a SELECT I made via SQL ALCHEMY . But when I use return it only returns the first ROW of SELECT .

The funny thing is that with "print" it displays all results without any problems.

Could anyone help me in this mystery?

def lista_unidades():
uni = session.query(UnidadesMam).all()
for u in uni:
    return json.dumps({'Unidade': u.nomeUni, 'Endereco': u.enderecoUni, 'Bairro': u.bairroUni, 'Cep': u.cepUni,
                        'Cidade': u.cidadeUni, 'Estado': u.estadoUni, 'Pais': u.paisUni})
    
asked by anonymous 17.11.2016 / 17:39

1 answer

0

This is due to return , which in the first loop is immediately invoked and the function where it is contained, in this case the lista_unidades() , is terminated / terminated, do the following:

def lista_unidades():
    uni = session.query(UnidadesMam).all()
    dados = []
    for u in uni:
        dados.append({'Unidade': u.nomeUni, 'Endereco': u.enderecoUni, 'Bairro': u.bairroUni, 'Cep': u.cepUni, 'Cidade': u.cidadeUni, 'Estado': u.estadoUni, 'Pais': u.paisUni})
    return json.dumps(dados)
    
17.11.2016 / 17:42