| | 335 | |
| | 336 | == Settings inheritance == |
| | 337 | |
| | 338 | I ([http://djangopeople.net/cordis Sergey Yushckeyev]) have extended proposed solution: |
| | 339 | |
| | 340 | env |
| | 341 | {{{ |
| | 342 | export APP_SETTINGS = 'development.cordis' |
| | 343 | }}} |
| | 344 | |
| | 345 | settings/__init__.py |
| | 346 | {{{ |
| | 347 | #!python |
| | 348 | |
| | 349 | CONFIGURATION = 'common' |
| | 350 | |
| | 351 | def deep_update(from_dict, to_dict): |
| | 352 | for (key, value) in from_dict.iteritems(): |
| | 353 | if key in to_dict.keys() and isinstance(to_dict[key], dict) and isinstance(value, dict): |
| | 354 | deep_update(value, to_dict[key]) |
| | 355 | else: |
| | 356 | to_dict[key] = value |
| | 357 | |
| | 358 | try: |
| | 359 | modules = os.environ.get('APP_SETTINGS', '').split('.') |
| | 360 | current = __name__ |
| | 361 | for name in modules: |
| | 362 | module = getattr(__import__(current, globals(), locals(), [name]), name) |
| | 363 | current += '.' + name |
| | 364 | current_settings = {} |
| | 365 | for setting in dir(module): |
| | 366 | if setting == setting.upper(): |
| | 367 | current_settings[setting] = getattr(module, setting) |
| | 368 | deep_update(current_settings, locals()) |
| | 369 | except ImportError, e: |
| | 370 | print 'Unable to import configuration: %s' % (str(e)) |
| | 371 | }}} |
| | 372 | |
| | 373 | settings/development/cordis.py |
| | 374 | {{{ |
| | 375 | #!python |
| | 376 | |
| | 377 | CONFIGURATION = 'cordis' |
| | 378 | }}} |
| | 379 | |
| | 380 | app/models.py |
| | 381 | {{{ |
| | 382 | #!python |
| | 383 | |
| | 384 | from settings import CONFIGURATION |
| | 385 | print CONFIGURATION # prints 'cordis' |
| | 386 | }}} |