[Python] django settings per application - best practice?

Discussão em 'Python' iniciado por Stack, Setembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    this is somewhat related to this question
    Why is django's settings object a LazyObject?

    In my django project i have several applications. Each application can have its own non-trivial settings file.

    proj/
    proj/
    settings.py
    app/
    settings.py
    views.py


    What is the general best practice here?
    should app/settings.py do

    from django.conf import settings
    APP_SETTING= lambda: settings.getattr('APP_SETTING', 'custom_value')
    PROJ_SETTING= lambda: settings.PROJ_SETTING


    and then in app/views.py do

    import .settings
    X = settings.APP_SETTING
    Y = settings.PROJ_SETTING


    or should I be modifying the django lazy settings object in app/settings.py as per the django coding style?

    from django.conf import settings
    # not even sure how I would check for a default value that was specified in proj/settings.py
    settings.configure(APP_SETTING='custom_value')


    and then each app/views.py just consumes proj/settings.py via django.conf settings?

    from django.conf import settings
    X = settings.APP_SETTING
    Y = settings.PROJ_SETTING


    There are obviously quite a few other permutations but I think my intent is clear.
    Thanks in advance.

    Continue reading...

Compartilhe esta Página