Opened 3 years ago

Closed 3 years ago

#32764 closed New feature (duplicate)

pass getattr() for settings to the settings module

Reported by: Mike Brancato Owned by: nobody
Component: Core (Other) Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mike Brancato)

When the Settings class loads the global settings, it roughly does the following.

  1. Uses importlib to instantiate the settings module.
  2. Iterates thru the upper-case attributes of the module.
  3. Assigns the value of the module attribute to an attribute on the Settings class instance.

setting_value = getattr(mod, setting)

This effectively makes dynamic settings impossible. There are probably many use cases for settings which change during runtime, my use-case is dynamic credentials that rotate periodically. So I've used features from PEP-562 (requires Python 3.7) to make certain attributes of a module dynamic. The only requirement really is that getattr() be called on the module.

Is there any reason that the Settings class could not instead maintain a list of settings and use a __getattr__ method to check known settings and pass those to the module?

Change History (2)

comment:1 by Mike Brancato, 3 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 3 years ago

Component: UncategorizedCore (Other)
Resolution: duplicate
Status: newclosed

Is there any reason that the Settings class could not instead maintain a list of settings

I don't think it's feasible, settings are not limited to Django's internal use only. You can define your own settings, 3rd party package can define their settings etc. I would recommend to start a discussion about using PEP-562 in settings on DevelopersMailingList where you'll reach a wider audience.

my use-case is dynamic credentials that rotate periodically.

See #30178.

Closing as a duplicate.

Note: See TracTickets for help on using tickets.
Back to Top