#๐Ÿ”’ pleasae review the fast api code

17 messages ยท Page 1 of 1 (latest)

open sapphire
open zodiacBOT
#

@open sapphire

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

smoky sparrow
#

why do you have this in your main.py? ```py
client = AsyncIOMotorClient(settings.MONGO_URI)
database = client['....']
product_collection = database.get_collection('products')

#
class ProductResponse(BaseModel):
    products: List[Product]

@router.get("/api/v1/getproduct", response_model=List[ProductResponse])
``` you are defining the response model as a `List[List[Product]]`

just use `List[Product]` directly in the `@router` and delete the ProductResponse class
#

also never use pydantic.v1

#

if you're using a modern python version, you can use list[thing] instead of List[thing] (3.9+) and thing | None instead of Optional[thing] (3.10+)

#

and lastly, I personally like to use FastAPI Dependencies to access the collection instead of just declaring it globally
(helps a lot with automated testing too, which you absolutely must have)

open sapphire
open sapphire
fathom wadi
open sapphire
#

didn't get it

#

can you give changed code

open sapphire
#

appreciated

open zodiacBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.