Skip to content
Myr

Merge pull request #2854 from dougthor42/update-docs-to-be-more-clear-about-from_config-instantiation-#2853

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Update docs to be more clear about from config instantiation

authored by David Lord · committed by GitHub · parents ff56126, 830c77c · Browse files · 2 files+42 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+42 added−0 removed

  • other:+42 added−0 removed

2 files+42 added−0 removed

modified:docs/config.rst+38 added
551 unmodified lines
552:meth:`~flask.Config.from_object`::553554 app.config.from_object('configmodule.ProductionConfig')555556Note that :meth:`~flask.Config.from_object` does not instantiate the class557object. If you need to instantiate the class, such as to access a property,558then you must do so before calling :meth:`~flask.Config.from_object`::559560 from configmodule import ProductionConfig561 app.config.from_object(ProductionConfig())562563 # Alternatively, import via string:564 from werkzeug.utils import import_string565 cfg = import_string('configmodule.ProductionConfig')()566 app.config.from_object(cfg)567568Instantiating the configutation object allows you to use ``@property`` in569your configuration classes::570571 class Config(object):572 """Base config, uses staging database server."""573 DEBUG = False574 TESTING = False575 DB_SERVER = '192.168.1.56'576577 @property578 def DATABASE_URI(self): # Note: all caps579 return 'mysql://user@{}/foo'.format(self.DB_SERVER)580581 class ProductionConfig(Config):582 """Uses production database server."""583 DB_SERVER = '192.168.19.32'584585 class DevelopmentConfig(Config):586 DB_SERVER = 'localhost'587 DEBUG = True588589 class TestingConfig(Config):590 DB_SERVER = 'localhost'591 DEBUG = True592 DATABASE_URI = 'sqlite:///:memory:'593594There are many different ways and it's up to you how you want to manage595your configuration files. However here a list of good recommendations:596597- Keep a default configuration in version control. Either populate the
99 unmodified lines
modified:flask/config.py+4 added
154 unmodified lines
155 app.config.from_object('yourapplication.default_config')156 from yourapplication import default_config157 app.config.from_object(default_config)158159 Nothing is done to the object before loading. If the object is a160 class and has ``@property`` attributes, it needs to be161 instantiated before being passed to this method.162163 You should not use this function to load the actual configuration but164 rather configuration defaults. The actual config should be loaded165 with :meth:`from_pyfile` and ideally from a location not within the166 package because the package might be installed system wide.
103 unmodified lines