Skip to content

adamchainz/django-load

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-load

Module and object loader utilities for Django.

Features

Examples

For full API documentation, please refer to http://django-load.readthedocs.org.

Let's assume your app wants to load all plugins.py files from the installed apps, to allow those apps to extend your application. You can achieve this like this:

from django_load.core import load

load('plugins')

Now let's say you want to do the same, but actually do something with those modules, more specific, find all objects in those modules, that are subclasses of BasePlugin and call our do_something function with those objects:

from django_load.core import iterload

for module in iterload('plugins'):
    for name in dir(module):
        obj = getattr(module, name)
        if issubclass(obj, BasePlugin):
            do_something(obj)

You could also have a setting called MY_APP_PLUGINS which contains import paths similar to MIDDLEWARE_CLASSES. You want to load those plugins and call the do_something function with them:

from django_load.core import iterload_objects
from django.conf import settings

for obj in iterload_object(settings.MY_APP_PLUGINS):
    do_something(obj)

If you only want to load a single object, you can do that too. Let's say you want to load MyObject from the mypackage.mymodule module:

from django_load.core import load_object

obj = load_object('mypackage.mymodule.MyObject')

About

loader utilities for Django

Resources

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages

  • Python 100.0%