From 3883cc4fe4d5874e2cec6521633d45779b11a4d1 Mon Sep 17 00:00:00 2001 From: Pavel Savchenko Date: Tue, 27 Feb 2018 21:43:18 +0100 Subject: [PATCH] Docs: explicitly describe property in patterns.rst (#189) Hopefully this saves time for new users of django-configuration (like myself), who "just needed" to lazily evaluate a string inside a dictionary. This doubles as an example for `RAVEN_CONFIG` which was the whole reason I was here in the first place... The actual problem I faced was that a setting remains of type `values.Value` when nested inside a dictionary. Which results in a weird issues like this: ``` 2018-02-24T20:59:26.125208+00:00 app[web.1]: Traceback (most recent call last): 2018-02-24T20:59:26.125364+00:00 app[web.1]: self.client.http_context(self.get_http_context(environ)) 2018-02-24T20:59:26.125215+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/raven/middleware.py", line 98, in __call__ 2018-02-24T20:59:26.125368+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/raven/contrib/django/models.py", line 54, in 2018-02-24T20:59:26.125482+00:00 app[web.1]: __getattr__ = lambda x, o: getattr(get_client(), o) 2018-02-24T20:59:26.125486+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/raven/contrib/django/models.py", line 134, in get_client 2018-02-24T20:59:26.125613+00:00 app[web.1]: instance = Client(**options) 2018-02-24T20:59:26.125618+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/raven/contrib/django/client.py", line 147, in __init__ 2018-02-24T20:59:26.125769+00:00 app[web.1]: Client.__init__(self, *args, **kwargs) 2018-02-24T20:59:26.125771+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/raven/base.py", line 171, in __init__ 2018-02-24T20:59:26.125927+00:00 app[web.1]: self.set_dsn(dsn, transport) 2018-02-24T20:59:26.125929+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/raven/base.py", line 251, in set_dsn 2018-02-24T20:59:26.126063+00:00 app[web.1]: if dsn not in self._transport_cache: 2018-02-24T20:59:26.126076+00:00 app[web.1]: TypeError: unhashable type: 'Value' ``` --- docs/patterns.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/patterns.rst b/docs/patterns.rst index 852eab2..2a2efa1 100644 --- a/docs/patterns.rst +++ b/docs/patterns.rst @@ -38,6 +38,22 @@ command line option, e.g.:: python manage.py runserver --settings=mysite.settings --configuration=Prod +Property settings +----------------- + +Use a `property` to allow for computed settings. This pattern can also be used to postpone / lazy evaluate a value. E.g. useful when nesting a Value in a dictionary and a string is required:: + + class Prod(Configuration): + SENTRY_DSN = values.Value(None, environ_prefix=None) + + @property + def RAVEN_CONFIG(self): + return { + 'dsn': self.SENTRY_DSN, + } + + + Global settings defaults ------------------------