Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

python - Django default settings convention for pluggable app?

What's a djangonautic way of handling default settings in an app if one isn't defined in settings.py?

I've currently placed a default_settings file in the app and I've considered a few options. I'm leaning towards the first option, but there may be pitfalls I'm not aware of in using globals()

I've mostly seen apps do a FOO = getattr(settings, 'FOO', False) at the top of the file that uses the setting but I think there are readability/repetition problems with this approach if the values / names are long.


1: Place settings in a function and iterate over locals / set globals

def setup_defaults():
    FOO = 'bar'
    for key, value in locals().items():
        globals()[key] = getattr(settings, key, value)

setup_defaults()

Pros:

  • Only have to write var name once to pull default of same name from django settings.

Cons:

  • Not used to using globals() and don't know of any implications

2: Write getattr(settings, 'MY_SETTING', default_settings.MY_SETTING) every call

Pros: - Very clear.

Cons: - Repetitive


3: Always define settings as FOO = getattr(settings, 'FOO', '...setting here...')

Pros: - Defaults are always overridden

Cons:

  • Repetitive (must define var twice - once in string form, once in var)
  • Setting is not as readable since it's now the third argument

4: Create utility function to get_or_default(setting)

Pros:

  • Simple
  • Don't have to repeat string representation of setting

Cons:

  • Have to call it

5: Create a settings class

class Settings(object):
    FOO = 'bar'

    def __init__(self):
         # filter out the startswith('__') of 
         # self.__dict__.items() / compare to django.conf.settings?

my_settings = Settings()

Cons:

  • Can't do from foo.bar.my_settings import FOO (actually, that's a terrible deal breaker!)

I'd love to hear feedback.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think it's quite common to create a settings.py in your app's package, where you define your settings like this:

from django.conf import settings
FOO = getattr(settings, 'FOO', "default_value")

In your app you can import them from your app's settings module:

from myapp.settings import *

def print_foo():
    print FOO

But I think everybody agrees that Django is lacking a better generic architecture for this! If you're looking for a more sophisticated way to handle this, there are some third party apps for this like django-appconf, but it's your decision if you want to introduce one more dependency for your app or not!

Updated for 2020

In settings.py, put settings.* before the property.

from django.conf import settings
settings.FOO = getattr(settings, 'FOO', "default_value")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...