Fastapi - How to override BaseSettings of my library

Viewed 18

I have an app with these settings:

class Settings(BaseSettings):
    local: str = "test"


settings = Settings()

I have a library that is imported to the app:

class Settings(BaseSettings):
    local: str = "no_local_set"


settings = Settings()

When i print local in my app it's print me 'no_local_set'. There is a way to override BaseSettings in my main app, and this settings will be use everywhere on my librairies.

My main purpose here is the lib will be use in many different app. So the 'local' variable will be different depending of app that use the library.

1 Answers

In your main you could solve it like this:

#import Settings from <libaryname>

class MainSettings(Settings):
  def __init__(self):
    self.local = "test"

settings = MainSettings()

Now you've overwritten the Settings class and added specific settings.

I would remove

settings = Settings()

from your library.

Related