Question

I'm writing reusable Django app. What are best practices for adding own settings into settings.py ? Do I need to prefix every setting with my app_name, or maybe I should make only one setting dict. for my app?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top