Login

Dynamic Backends

Author:
LorenDavie
Posted:
September 28, 2008
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

This allows various implementations of a common interface to be loaded. Back end modules can be specified in settings.py, and from there be loaded and treated polymorphically by an application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# ================================
# = Dynamic Backends for Modules =
# ================================
def load_backend(setting_name,default_name=None):
    """
    Dynamically loads a backend module from the specified django settings
    name, or falling back to the default value if the settings is empty
    or unspecified in the settings.
    """
    backend_name = None
    from django.conf import settings as django_settings
    if hasattr(django_settings,setting_name):
        backend_name = getattr(django_settings,setting_name) or default_name
    else:
        backend_name = default_name
    
    if backend_name:
        return __import__(backend_name,'','',[''])
    else:
        raise ImportError, 'No backend module specified for %s in Django settings, and no default available.' % setting_name

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 3 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.