Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return lists #56

Closed
DaviOtero opened this issue Feb 25, 2019 · 7 comments
Closed

Return lists #56

DaviOtero opened this issue Feb 25, 2019 · 7 comments
Labels
question Question or problem question-migrate

Comments

@DaviOtero
Copy link

Hi there!

First of all, gerat project! Thank You for sharing it!

i'm a somewhat a beginner, i'd like some help if you guys don't mind. (I couldn't find another forum for questions)

I've managed to read a list of objects inside a json, by passing the body like this:

[
  {
    "name": "Item name example 1",
    "price": 100,
    "is_offer": true
  },
  {
    "name": "Item name example 2",
    "price": 100,
    "is_offer": true
  },
  {
    "name": "Item name example 3",
    "price": 100,
    "is_offer": true
  }
]

The objective of my micro example is to receive a list of Items (the same base class of the API example), do some processing (change price value by a fixed factor) and return that altered list.

this works:

@app.put("/items/{item_id}")
def create_item(item_id: int, items: List[Item]):    
    return {"total" : len(items)}

and i can iterate trough the list with

for item in items:

but when i try to return the list with

return {items}

i get "TypeError: unhashable type: 'list'" error.

is there a simple way to return the list? Or i'll need to create the return string manually?

@DaviOtero DaviOtero added the question Question or problem label Feb 25, 2019
@tiangolo
Copy link
Owner

Awesome! I'm glad you're liking it.

You should be able to:

return items

or:

return {"items": items}

@euri10
Copy link
Contributor

euri10 commented Feb 25, 2019

You may (or I may misunderstand what you want to do) be mixing response_model and query parameters

  • to return a list you should have a response_model in the route @app.put("/items/{item_id}", response_model=List[Item])
  • the items: List[Item] part means you want a list of items in the query passed in the put
  • the curly braces around items most likely cast items to a set (not sure about that)

@DaviOtero
Copy link
Author

return items

God... it's so simple, i feel bad for not thinking of it.

Thank you guys!

@tiangolo
Copy link
Owner

tiangolo commented Mar 2, 2019

Awesome @DaviOtero ! 🎉


As always, thanks @euri10 ! 👏 🌮 🍰

@zopefiend
Copy link

zopefiend commented Aug 5, 2020

Working through the tutorial ( https://fastapi.tiangolo.com/tutorial/ ), hit stumbling block on first try. The command reads:
uvicorn main:app --reload
Is there some environment requirements that are not mentioned? Because that returns:
" 'uvicorn' is not recognized as an internal or external command,
operable program or batch file."
OK, so yes, I'm on a Windows desktop, That's what the employer provides ...
With uvicorn, fastapi, pydantic, etc. in /Lib/site-packages, I'm thinking that this should be equvalent:
[python directory]>python -m uvicorn main.py:main --reload
which tries to start, then error:
Error loading ASGI app. Could not import module "main.py".
OK, how about:
[python directory]>python -m uvicorn uvicorn.main.py:main --reload
Error loading ASGI app. Could not import module "uvicorn.main.py".
No? Try this:
[python directory]>python -m uvicorn. main.py
Error loading ASGI app. Import string "main.py" must be in format ":"
Right, that's why I had it the other way .., Once again:
[python directory]>python -m uvicorn uvicorn:main.py --reload
Error loading ASGI app. Attribute "main.py" not found in module "uvicorn"
Really odd, I'm looking right at it, in file explorer ...
Anyhow, pretty much stopped at the first command in the tutorial, thanks for any hints.

BTW, this editor delates some characters ( such as uderscores, around main ), and even whole words, so I won't be surprised if you can't read this as I typed it.

@ruoshui-git
Copy link

ruoshui-git commented Nov 30, 2020

@zopefiend Maybe try python -m uvicorn main:main --reload
But make sure in you code, you did:

main = FastAPI()

If you did:

app = FastAPI()

you should run

python -m uvicorn main:app --reload

In main:main, the first main is the package name, or your file name in this case, and the second main is the var/obj name in the package.

If you installed them in a virtual environment, you should be able to do just unicorn main:main --reload.

@zopefiend
Copy link

zopefiend commented Nov 30, 2020 via email

@tiangolo tiangolo changed the title [QUESTION] Return lists Return lists Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8227 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

5 participants