有没有办法获得当前应用程序的app_config?

时间:2015-01-12 14:19:56

标签: python django

我想为当前应用获取一个Django app_config实例。

例如:

baseapp / views.py

class MyBaseView(RedirectView):

    def get_redirect_url(self):
        # That's the hypothetical method I would like to have
        app_config = apps.get_current_app_config()

        return app_config.url 

APP1 / views.py

class MyView1(MyBaseView):
    pass

APP1 / apps.py

class MyApp1Config(AppConfig):
    name = 'myapp1'
    verbose_name = 'My Application 1'
    url = 'http://example.com/'

APP2 / views.py

class MyView2(MyBaseView):
    pass

APP2 / apps.py

class MyApp2Config(AppConfig):
    name = 'myapp2'
    verbose_name = 'My Application 2'
    url = 'http://example.org/'

基本上我想让超类能够使用子类信息来执行某些操作。重定向只是一个例子,因为我实际上做了更复杂的事情并从子类中读取更多数据。

1 个答案:

答案 0 :(得分:1)

我只需要做类似的事情,以下工作;

from .apps import MyApp1Config
from django.apps import apps 

app_config = apps.get_app_config(MyApp1Config.name)