Replies: 5 comments 4 replies
-
I'll shamelessly self-bump this. Why I think being able to change |
Beta Was this translation helpful? Give feedback.
-
I would like to put some effort into implementing this myself, if this has some potential to be included in a future versions of pydantic. Any thoughts on this? |
Beta Was this translation helpful? Give feedback.
-
I'd love/need that as well. I tried everything but nothing I came up with worked? ` class Config: Setting.Config = Config |
Beta Was this translation helpful? Give feedback.
-
If anyone is also interested in this, I've found out that we need to do two things in order for it to work:
I've put together a hack that works by doing something like this (yes, I know it is fugly): import pydantic
class MySettings(pydantic.BaseSettings):
param_a: bool
runtime_env_prefix = "something_set_at_runtime"
# 1. override the original env_prefix
MySettings.__config__.env_prefix = runtime_env_prefix
# 2. prepare fields again with the new env_prefix in place
for field in MySettings.__fields__.values():
MySettings.__config__.prepare_field(field)
# now we can create our settings and it will look for env variables with the runtime env prefix
settings = MySettings() |
Beta Was this translation helpful? Give feedback.
-
Running pydantic-settings 2.0.0 - I use inheritance to do this. For me it works better, as I don't need to worry about which import pydantic_settings
class FooLikeSettings(pydantic_settings.BaseSettings):
model_config = (
pydantic_settings.SettingsConfigDict(
env_prefix="foo_",
)
... # model definition
class Bar(FooLikeSettings):
pass
Bar.model_config["env_prefix"] = "bar_" Not sure if this is the right thing. Probably this should be discouraged in favor of creating nested models. |
Beta Was this translation helpful? Give feedback.
-
Hi, is it possible to change
env_prefix
at runtime? I've tried some hacks which didn't work (playing with inheritance, overriding) but noting worked. Something like this would be nice:Beta Was this translation helpful? Give feedback.
All reactions