开发者

How to create Django Apps with life cycle?

We are developing a huge application on Django contains lot of apps with dependency in between. We want to implement a life-cycle(install,onload,unload,uninstall) for apps, since it is must have feature(after deplo开发者_开发技巧yment); is there any frameworks or packages existed ?

What is the best way to implement this ?

Thanks


It looks like you want to replicate features of other popular languages and frameworks in Django. Reading your post OSGi springs to mind.

The problem is that 99,9% of all Django applications are not "dynamic" in the sense that apps can be installed, loaded, unloaded and uninstalled at runtime. That's not how Django applications work. You install an app before the application is started. To uninstall an app you shutdown your application and remove it from the INSTALLED_APPS (settings.py) list. Since this does not happen at runtime there won't be any signals, callbacks or other things to the Django application.

But there are certain things you can do to at least meet some of your needs. From the top of my head I think you could do something like a before_install and after_install "signal" (not in the Django signal sense).

To do so you could write some sort of "controller" or "registry" app. One for the before_install and one for after_install code. Those apps would sit before and after the rest of your apps in INSTALLED_APPS.

e.g.

INSTALLED_APPS = (
    # all the Django apps and other third party apps
    'before_install_controller_app',
    # all your in-house apps
    'after_install_controller_app',
)

These two apps iterate all the apps listed under INSTALLED_APPS. They try to import a specific python file from each of the apps. If the file is not in the app (because it's a Django or third-party app) nothing happens. But if there is a file, let's call it before_install.py and after_install.py it is loaded and the code inside is run.

The same would apply for the after_install_controller_app app, but it would look for after_install.py.

It's basically the same mechanism Django uses for it's admin. The admin app iterates all the apps and tries to import admin.py. That's why you should at first have a look at the corresponding Django source. It's pretty straight forward and easy to understand. I did something similar and it worked like a charm.

There are of course some limitations. For instance:

  • The before_install.py code would not have access to database models of the underlying app because they may be a) not have been created yet (see order of the apps) or b) out of date.
  • I am sure there are many more, but at the moment I don't find any more.

On last thing:

Don't try to hard to map/port features/paradigms of one programming language/frameworks to another. There might be fundamental differences that render good approaches in one language/framework useless in another. That certainly seems the case here.

If you have further questions I'd be glad to help if I can.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜