how to modularize django settings.py?

后端 未结 8 727
礼貌的吻别
礼貌的吻别 2020-12-13 19:46

When you install a new django application, you have to add/modify your settings.py module.

For a project I\'m trying to make that module a python subpackage and crea

8条回答
  •  感情败类
    2020-12-13 20:21

    If you prefer more magic than in my previous more_settings.modify() approach, try this:

    settings.py:

    INSTALLED_APPS = ('whatever',)
    import more_settings
    more_settings.modify(globals())
    

    more_settings.py:

    def config(INSTALLED_APPS=(), **other_settings):
        INSTALLED_APPS += ('another_app',)
        del other_settings
        return locals()
    
    def modify(settings):
        settings.update(config(**settings))
    

    Pros: no need to refer to settings with dict notation

    Cons: must define modified settings as kwargs for config()

提交回复
热议问题