document that Config.from_object uses object as-is
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Thor · committed by David Lord · parent ff56126 · 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
Provenance
- Commit
830c77c- Tree
b3eefb0- Author
Douglas Thor <doug.thor@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
ff56126
2 files changed+42 added−0 removed
modified:docs/config.rst+38 added
551 unmodified lines552 :meth:`~flask.Config.from_object`::553 554 app.config.from_object('configmodule.ProductionConfig')555 556+Note that :meth:`~flask.Config.from_object` does not instantiate the class557+object. If you need to instantiate the class, such as to access a property,558+then you must do so before calling :meth:`~flask.Config.from_object`::559+560+ from configmodule import ProductionConfig561+ app.config.from_object(ProductionConfig())562+563+ # Alternatively, import via string:564+ from werkzeug.utils import import_string565+ cfg = import_string('configmodule.ProductionConfig')()566+ app.config.from_object(cfg)567+568+Instantiating the configutation object allows you to use ``@property`` in569+your configuration classes::570+571+ class Config(object):572+ """Base config, uses staging database server."""573+ DEBUG = False574+ TESTING = False575+ DB_SERVER = '192.168.1.56'576+577+ @property578+ def DATABASE_URI(self): # Note: all caps579+ return 'mysql://user@{}/foo'.format(self.DB_SERVER)580+581+ class ProductionConfig(Config):582+ """Uses production database server."""583+ DB_SERVER = '192.168.19.32'584+585+ class DevelopmentConfig(Config):586+ DB_SERVER = 'localhost'587+ DEBUG = True588+589+ class TestingConfig(Config):590+ DB_SERVER = 'localhost'591+ DEBUG = True592+ DATABASE_URI = 'sqlite:///:memory:'593+594 There are many different ways and it's up to you how you want to manage595 your configuration files. However here a list of good recommendations:596 597 - Keep a default configuration in version control. Either populate the99 unmodified linesmodified:flask/config.py+4 added
154 unmodified lines155 app.config.from_object('yourapplication.default_config')156 from yourapplication import default_config157 app.config.from_object(default_config)158 159+ 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.162+163 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