[Django]-Settings namespace in Django

3👍

You should try to define app settings only in the package involved, especially if you are considering a reusable package. Sticking with unique global names (which settings are infact) is always a good idea. Have a look at PEP-8 globals.

If you’re creating a reusable package its good practice to first try to import the attribute from the settings module to see if the user is overriding it with its own constant.

E.g.

from django.conf import settings

MY_CONSTANT = getattr(settings, 'MY_CONSTANT', True)

def foo(*args, **kwargs):
    if MY_CONSTANT:
        return 'hello'
    else:
        return 'goodbye'

This way the user could set MY_CONSTANT = False in his settings module so the app will return 'goodbye' instead of the default 'hello'

Most packages do use the package name as a prefix as you suggested, here’s a few examples:

FEINCMS, tastypie

Leave a comment