fastapi/docs/em/docs/tutorial/dependencies/classes-as-dependencies.md

4.5 KiB
Raw Blame History

🎓 🔗

🤿 🔘 🔗 💉 ⚙️, ➡️⏮️ 🖼.

dict ➡️ ⏮️ 🖼

⏮️ 🖼, 👥 🛬 dict ➡️ 👆 🔗 ("☑"):

{* ../../docs_src/dependencies/tutorial001.py hl[9] *}

⤴️ 👥 🤚 dict 🔢 commons 🛠️ 🔢.

& 👥 💭 👈 👨‍🎨 💪 🚫 🚚 📚 🐕‍🦺 (💖 🛠️) dictⓂ, ↩️ 👫 💪 🚫 💭 👫 🔑 & 💲 🆎.

👥 💪 👍...

🔗

🆙 🔜 👆 ✔️ 👀 🔗 📣 🔢.

👈 🚫 🕴 🌌 📣 🔗 (👐 🔜 🎲 🌖 ⚠).

🔑👈 🔗 🔜 "🇧🇲".

"🇧🇲" 🐍 🕳 👈 🐍 💪 "🤙" 💖 🔢.

, 🚥 👆 ✔️ 🎚 something (👈 💪 🚫 🔢) & 👆 💪 "🤙" (🛠️ ) 💖:

something()

⚖️

something(some_argument, some_keyword_argument="foo")

⤴️ "🇧🇲".

🎓 🔗

👆 5📆 👀 👈👐 🐍 🎓, 👆 ⚙️ 👈 🎏 .

🖼:

class Cat:
    def __init__(self, name: str):
        self.name = name


fluffy = Cat(name="Mr Fluffy")

👉 💼, fluffy 👐 🎓 Cat.

& ✍ fluffy, 👆 "🤙" Cat.

, 🐍 🎓 🇧🇲.

⤴️, FastAPI, 👆 💪 ⚙️ 🐍 🎓 🔗.

FastAPI 🤙 👈 "🇧🇲" (🔢, 🎓 ⚖️ 🕳 🙆) & 🔢 🔬.

🚥 👆 🚶‍♀️ "🇧🇲" 🔗 FastAPI, 🔜 🔬 🔢 👈 "🇧🇲", & 🛠️ 👫 🎏 🌌 🔢 🛠️ 🔢. 🎧-🔗.

👈🇧🇲 ⏮️ 🙅‍♂ 🔢 🌐. 🎏 🔜 🛠️ 🔢 ⏮️ 🙅‍♂ 🔢.

⤴️, 👥 💪 🔀 🔗 "☑" common_parameters ➡️ 🔛 🎓 CommonQueryParams:

{* ../../docs_src/dependencies/tutorial002.py hl[11:15] *}

💸 🙋 __init__ 👩‍🔬 ⚙️👐 🎓:

{* ../../docs_src/dependencies/tutorial002.py hl[12] *}

... ✔️ 🎏 🔢 👆 ⏮️ common_parameters:

{* ../../docs_src/dependencies/tutorial001.py hl[9] *}

📚 🔢 FastAPI 🔜 ⚙️ "" 🔗.

👯‍♂️ 💼, 🔜 ✔️:

  • 📦 q 🔢 🔢 👈 str.
  • skip 🔢 🔢 👈 int, ⏮️ 🔢 0.
  • limit 🔢 🔢 👈 int, ⏮️ 🔢 100.

👯‍♂️ 💼 💽 🔜 🗜, ✔, 📄 🔛 🗄 🔗, .

⚙️

🔜 👆 💪 📣 👆 🔗 ⚙️ 👉 🎓.

{* ../../docs_src/dependencies/tutorial002.py hl[19] *}

FastAPI 🤙 CommonQueryParams 🎓. 👉 ✍ "👐" 👈 🎓 & 👐 🔜 🚶‍♀️ 🔢 commons 👆 🔢.

🆎🆚 Depends

👀 👥CommonQueryParams 🕐 🔛 📟:

commons: CommonQueryParams = Depends(CommonQueryParams)

🏁 CommonQueryParams,:

... = Depends(CommonQueryParams)

... FastAPI 🔜 🤙 ⚙️ 💭 🔗.

➡️ 👈 FastAPI 🔜📣 🔢 & 👈 FastAPI 🔜 🤙 🤙.


👉 💼, 🥇 CommonQueryParams,:

commons: CommonQueryParams ...

...🚫 ✔️ 🙆 🎁 🔑 FastAPI. FastAPI 🏆 🚫 ⚙️ 💽 🛠️, 🔬, . ( ⚙️ = Depends(CommonQueryParams) 👈).

👆 💪 🤙 ✍:

commons = Depends(CommonQueryParams)

...:

{* ../../docs_src/dependencies/tutorial003.py hl[19] *}

📣 🆎 💡 👈 🌌 👆 👨‍🎨 🔜 💭 🔜 🚶‍♀️ 🔢 commons, & ⤴️ 💪 👆 ⏮️ 📟 🛠️, 🆎 , :

👆 👀 👈 👥 ✔️ 📟 🔁 📥, ✍ CommonQueryParams 🕐:

commons: CommonQueryParams = Depends(CommonQueryParams)

FastAPI 🚚👫 💼, 🌐 🔗 🎯 🎓 👈 FastAPI 🔜 "🤙" ✍ 👐 🎓 .

📚 🎯 💼, 👆 💪 📄:

↩️ ✍:

commons: CommonQueryParams = Depends(CommonQueryParams)

...👆 ✍:

commons: CommonQueryParams = Depends()

👆 📣 🔗 🆎 🔢, & 👆 ⚙️ Depends() 🚮 "🔢" 💲 (👈 ⏮️ =) 👈 🔢 🔢, 🍵 🙆 🔢 Depends(), ↩️ ✔️🌕 🎓 🔄 🔘 Depends(CommonQueryParams).

🎏 🖼 🔜 ⤴️ 👀 💖:

{* ../../docs_src/dependencies/tutorial004.py hl[19] *}

...& FastAPI 🔜 💭 .

/// tip

🚥 👈 😑 🌅 😨 🌘 👍, 🤷‍♂ , 👆 🚫 💪 .

⌨. ↩️ FastAPI 💅 🔃 🤝 👆 📉 📟 🔁.

///