1
- .. _Python: http://www.python.org/
2
- .. _Django: http://www.djangoproject.com/
3
- .. _staticfiles app: http://headjs.com/
4
-
5
1
=======================
6
2
django-dynamicsettings
7
3
=======================
37
33
38
34
39
35
40
- 3. Usage
36
+ 3. Setup
41
37
:::::::::::::::::::::::::::::::::
42
38
43
- 3.1. Setup
44
- ------------------------------
45
-
46
39
If you installed *django-dynamic-settings* you can already
47
40
use it like you are used to python modules. That lets you
48
41
make advantage of the internal caching for settings.
@@ -51,7 +44,7 @@ If you want to do more (save settings in the database,
51
44
check settings in the admin, run tests) you need to do the
52
45
following:
53
46
54
- 1.put it into your ``INSTALLED_APPS`` setting . Make sure
47
+ **1.** Put ``'dynamicsettings'`` into your ``INSTALLED_APPS``. Make sure
55
48
``django.contrib.admin`` (and requirements) is appearing in your
56
49
``INSTALLED_APPS`` setting as well, since its used by
57
50
*django-dynamic-settings*:
@@ -70,7 +63,7 @@ following:
70
63
)
71
64
72
65
73
- 2. Add a url handler to handle the admin views of
66
+ **2.** Add a url handler to handle the admin views of
74
67
*django-dynamic-settings*. This is built-in into the standard
75
68
django admin, so you could add something like this:
76
69
@@ -87,9 +80,9 @@ django admin, so you could add something like this:
87
80
3. Add settings:
88
81
89
82
If you have another app which has settings you want to include or
90
- your custom app which is using *django-dynamic-settings* has
91
- some custom settings you can include these modules to add them
92
- to be handled by *django-dynamic-settings*. For example :
83
+ your custom app is using *django-dynamic-settings* and has
84
+ some custom settings, you can include these modules to be handled
85
+ by *django-dynamic-settings*:
93
86
94
87
::
95
88
@@ -114,24 +107,22 @@ For example:
114
107
115
108
This would make ``MY_SETTING`` and ``DEFAULT_CACHE_DURATION``
116
109
editable in the database.
117
- There is one thing you need to keep in mind when making
118
- settings editable via admin:
119
110
120
- **App's which are not using django-dynamic-settings are not
111
+ **Note**: App's which are not using * django-dynamic-settings* are not
121
112
affected by the changes. That includes for example Django itself.
122
113
It doesn't make much sense to change a setting in the database
123
114
via admin which is used internally by Django. It will simply have
124
- no effect.**
115
+ no effect.
125
116
126
117
So the project or app which will make usage of *django-dynamic-settings*
127
118
great way of retrieving and setting the settings in the database
128
119
should use *django-dynamic-settings* instead of the usual way of
129
- retrieving settings via ``from django.conf import settings``. The
120
+ retrieving settings via ``from django.conf import settings``. See
130
121
the next section for usage examples.
131
122
132
123
133
- 3.2. Using django-dynamic-settings
134
- ------------------------------------------------------------
124
+ 4. Usage
125
+ :::::::::::::::::::::::::::::::::
135
126
136
127
137
128
*django-dynamic-settings* is used in a very similiar way you
@@ -156,48 +147,77 @@ After that you can use settings as you are used to. Examples:
156
147
login_redirect_url = settings.LOGIN_REDIRECT_URL
157
148
my_custom_setting = settings.MY_SETTING
158
149
159
-
160
- Additionally you have access to the following methods:
161
-
162
- ``settings.get(key, default)``: Retrieve a setting for a
163
- particular name (``key``) or return the ``default``
164
- (``None`` if emitted). Works like the python built-in
165
- ``dict.get()`` method Example usage:
166
150
167
- ::
168
151
169
- login_redirect_url = settings.get('LOGIN_REDIRECT_URL', '/')
170
- my_custom_setting = settings.get('MY_SETTING)
152
+ Additionally you have access to the following methods:
171
153
154
+ - ``settings.get(key, default)``: Retrieve a setting for a
155
+ particular name (``key``) or return the ``default``
156
+ (``None`` if emitted). Works like the python built-in
157
+ ``dict.get()`` method Example usage:
158
+
159
+ ::
160
+
161
+ login_redirect_url = settings.get('LOGIN_REDIRECT_URL', '/')
162
+ my_custom_setting = settings.get('MY_SETTING)
163
+
164
+
165
+ - ``settings.set(key, value, type)``: This is setting a setting
166
+ specified by ``key`` directly in the database without using the
167
+ admin interface. ``value`` is the new value of the setting and
168
+ ``type`` the python type. If ``type`` is not explicitly given
169
+ it will try to resolve the type from the given `value``. Returns
170
+ the new value if setting was successful. Raises
171
+ KeyError if the setting is not allowed to be changed due to
172
+ not defining it in ``DYNAMICSETTINGS_INCLUDE_SETTINGS``.
173
+ Examples:
174
+
175
+ ::
176
+
177
+ login_redirect_url = settings.set('LOGIN_REDIRECT_URL', '/home/')
178
+ my_custom_setting = settings.set('MY_SETTING', 73, 'int')
179
+
180
+
181
+ - ``settings.reset(key)``: If you saved a setting in the database you
182
+ can reset it (giving the name of the setting) to its original value via
183
+ this method. This method returns ``True`` if the reset was successful
184
+ and ``False`` if not (setting wasn't saved to the database for example).
185
+ Examples:
186
+
187
+ ::
188
+
189
+ login_redirect_url = settings.set('LOGIN_REDIRECT_URL', '/home/')
190
+ settings.reset('LOGIN_REDIRECT_URL')
191
+ print settings.LOGIN_REDIRECT_URL
192
+
193
+
194
+ - ``settings.dict(keys)``: Returns a dict representation of the settings.
195
+ If ``keys`` is ommitted all settings which are included into *django-dynamic-settings*
196
+ are part of the dict. If you just want to retrieve particular settings
197
+ you can provide names of the settings within ``keys`` (a list of strings).
198
+
199
+ - ``settings.is_in_db(key)``: Check if a particular setting given by
200
+ its name (``key``) is saved in the db or not. Returns ``True`` if
201
+ this is case, ``False`` otherwise.
202
+
203
+ - ``settings.can_change(key)``: Check if a particular setting given by
204
+ its name (``key``) can be changed (and saved in the database). This
205
+ returns ``True`` if the setting is provided in ``DYNAMICSETTINGS_INCLUDE_SETTINGS``,
206
+ ``False`` otherwise.
207
+
208
+
209
+ 5. Additional settings
210
+ :::::::::::::::::::::::::::::::::
172
211
173
- ``settings.set(key, value, type)``: This is setting a setting
174
- specified by ``key`` directly in the database without using the
175
- admin interface. ``value`` is the new value of the setting and
176
- ``type`` the python type. If ``type`` is not explicitly given
177
- it will try to resolve the type from the given `value``. Returns
178
- the new value if setting was successful. Raises
179
- KeyError if the setting is not allowed to be changed due to
180
- not defining it in ``DYNAMICSETTINGS_INCLUDE_SETTINGS``.
181
- Examples:
212
+ Settings within *django-dynamic-settings* are cached in case you
213
+ are using Django's cache framework. To define the timeout for the
214
+ cached settings you can set ``DYNAMICSETTINGS_CACHE_TIMEOUT``:
182
215
183
216
::
184
217
185
- login_redirect_url = settings.set('LOGIN_REDIRECT_URL', '/home/')
186
- my_custom_setting = settings.set('MY_SETTING', 73, 'int')
218
+ DYNAMICSETTINGS_CACHE_TIMEOUT = 60
187
219
188
220
189
221
190
-
191
- 3.3. Complex usage
192
- ------------------------------
193
-
194
-
195
- 3.4. Additional settings
196
- ------------------------------
197
-
198
-
199
-
200
-
201
-
202
-
203
-
222
+ .. _Python: http://www.python.org/
223
+ .. _Django: http://www.djangoproject.com/
0 commit comments