1

I searched for a long time how to create a "plugin" framework in Django, without success. You can add some support for plugins, but having apps with full support for models, urls etc. is a hard thing.

Let's start from the beginning. I started an application using Django that is wanted to be extended by plugins in the future. I don't know which features vendors will add to that application, so they must be able to add database models too.

If I now say, let a plugin be a Django app, I have the problem that Django apps are not loaded dynamically during startup. I have to manually and hardcodedly add the app into my settings.py, add the urls to urls.py etc.

So created a simple plugin system that

  • searches for django apps in a plugins/ directory, and includes them into the "INSTALLED_APPS" dict, by adding that code INTO settings.py directly
  • adds Interfaces that can be implemented by plugins to automatically add e.g. urls into the root urls.py by collecting that implementations automatically.

Everything works, but one thing: I'd like to add a "shadowed" Plugin model to my database that holds the metadata of the installed plugins. And here comes the problem: If I want to have an activated field of a plugin, and just load the plugin if in the database the corresponding model record is "activated" - it does not work: At the time of creating the INSTALLED_APPS list, you have no access to the database and can't query models. And later in the Django setup process, when models are loaded and available, you can't dynamically add apps to the INSTALLED_APPS variable or tell Django in another way to load a certain app additionally.

How can I achieve this? Remember: This question is not about how to create dynamically plugin apps in django - this is answered here already. I want to know how to dynamically decide if a (potentially malign) plugin should be loaded by querying the DB first for a flag.

nerdoc
  • 1,044
  • 10
  • 28
  • What have you tried so far? – Klaus D. Jul 08 '18 at 18:25
  • loading model from `settings.py` - too early in stage. loading anywhere later: apps are already loaded. Both do not work. One thing I think that *could* work: eithter calling `django.apps.registry.Apps.populate()` again (is idempotent) with extending the INSTALLED_APPS dynamically in a late process. – nerdoc Jul 08 '18 at 19:02
  • You can see the code at [gitlab](https://gitlab.com/nerdocs/medux/MedUX/blob/develop/medux/extensionsystem/pluginmanager.py). – nerdoc Jul 08 '18 at 19:08

0 Answers0