Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Django

Working with Django Plugins

2022-07-25

Django-plugins is a package that helps you to build apps more reusable. It is currently tested with Python 2.7, 3.2, 3.3, and 3.4 along with Django versions 1.7 and 1.8. It might well work with other versions.

By using 'Django-plugins', you can define, access plugins and plugin points.

Installation:

$ pip install django-plugins

Plugins are stored as python objects and synchronized to database called plugin models(PluginPoint, Plugin).

Both Plugin and plugin models has a name and title attributes. And each plugin point/plugin can be marked as enabled/disabled/removed using 'status' field in respective models from Django admin panel.

Plugins are the classes which are hardcoded, which means they cannot be modified directly by the users. But they can change the database objects of those plugins. In this way, you can provide the flexibility to the user to change plugins.

NOTE: You should always use plugin attributes (name, title) from model objects but not from plugins.

All defined plugins and plugin points are synchronized to database tables using Django management command 'sync plugins' or 'sync DB'. 'sync plugins' command detects the removed plugin points, plugins and marks them as 'REMOVED' in the database.

Features of 'Django-plugins':

  • Synchronization with the database.
  • Plugin management from Django admin - enable/disable, order.
  • Many ways to access plugins and related plugin models - access plugins from views, templates etc.

All plugin points and plugins must reside in 'plugins.py' file in your Django app.

To register/create a plugin point:

To create a plugin point, you need to sub-class 'PluginPoint' which exists in Django plugins point module.

Example:

from djangoplugins.point import PluginPoint

class TestPluginPoint(PluginPoint):
    pass

Now, TestPluginPoint serves as a mount point for all its plugins. Now we have a point, we can start registering/creating plugins for it.

You can also define methods, attributes same as you would do for other classes. In turn, they will get inherited by each of its plugins.

Registering a plugin:

All individual plugins of this point need to subclass this so that they are registered as plugins for the plugin point, which means that subclassing plugin-point itself registers the plugin.

Example:

class TestPlugin(TestPluginPoint):
        name = 'test-plugin-1'
        title = 'Test Plugin 1'

Note: All plugins must define the name, title attributes.

Utilizing plugins:

As explained in the above section, each plugin is linked to the database record of 'Plugin' model in Django plugin. So the plugin model provides all database possibilities like filtering, sorting, searching.

* To get query set of all plugins related to plugin point, just import the plugin point and you can use 'get_plugins_qs' method.

TestPluginPoint.get_plugins_qs()

Note: This method return only enabled/active plugins.

If you need to sort or filter plugins, you should always access them via Django ORM.

TestPluginPoint.get_plugins_qs().order_by("title")

You can also access plugins in templates using 'get_plugins' template tag

{% load plugins %}

{% get_plugins your_app_name.plugins.TestPluginPoint as plugins %}

<ul>

    {% for plugin in plugins %}

        <li> {{ plugin.title }} </li>

    {% endfor %}

</ul>

To get the model instance of plugin point or plugin at any point of time, you can 'get_model' method

# Get model instance of a plugin point
testpluginpoint_obj = TestPluginPoint.get_model()

# Get model instance from plugin class.
testplugin_obj = TestPlugin.get_model()

# Get model instance by plugin name.
testplugin_obj = TestPluginPoint.get_model('test-plugin-1')

Note: 'get_model' method raises 'ObjectDoesNotExist' exception, if the object is not found.

For more details, you can refer Django-plugins official documentation here