Is it possible/necessary to pass argument to dependencies? #7704
Replies: 5 comments
-
Have you read https://fastapi.tiangolo.com/advanced/advanced-dependencies/#parameterized-dependencies? |
Beta Was this translation helpful? Give feedback.
-
Yes, the "dependency class" actually referring to this method. Sorry for the confusion. |
Beta Was this translation helpful? Give feedback.
-
I might be wrong, but I think the reason to use the class-based approach and instances of those classes instead of passing in args each time is that it's easier to cache the dependencies, which is a very useful feature and usually something you want to do. So generally I think it's better to use that approach, but if you really want to do it dynamically each time, is there a reason that |
Beta Was this translation helpful? Give feedback.
-
Well, that works for me, just curious XD. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help here @phy25 and @acnebs ! 🍰 🚀 Yeah, @itzMeerkat that would be the way to do it. On the other side, if you are dealing with security / OAuth2 scopes you can use https://fastapi.tiangolo.com/advanced/security/oauth2-scopes/ Anyway, thanks for reporting back and closing the issue! 👍 |
Beta Was this translation helpful? Give feedback.
-
First check
Description
As title.
For example, I want to give different API different access restrictions (like permission level). My authentication is separated from the application so what I have are uid and corresponding user information.
Now I want to use
Depends(check_token_and_permission)
to check if a certain request is valid. It seems not possible to pass an extra argument to the dependency function. I've read through the document and realized we have a workaround by creating a dependency class. However, I think it's less flexible than if we can doDepends(check_token_and_permission, 'admin')
and the function will be called as
check_token_and_permission(a,b,c,*args)
where our argument 'admin' goes into *args
My question is, is that possible? Or is my reasoning correct? If not, why current implementation is better?
Thank you so much for answering my question.
Beta Was this translation helpful? Give feedback.
All reactions