Developing Reusable Django Apps: App Settings
First part is here: Developing Reusable Django Apps.
Eventually your app will need some sort of configuration. Supplying many parameters to customise your views, template tags and filters to allow template authors easily harness the power of your app and implementing registration pattern are all sensible things to do. But at some point you will need configuration for your app at project level. Because app level configuration is not reusable. Consumers of your app should never have to change its source code. And what better place to put our configuration statements than settings.py? Remember; we want to make things easier for our app’s consumers, not harder. There is no need to add a new file to the project for a few lines of settings[1].
We already know we shouldn’t import settings.py directly. Instead we import the settings object (much like a singleton) of django.conf module:
from django.conf import settings
Now you can access different configuration options as attributes on this object. But I suggest you to use getattr() in order to avoid getting AttributeErrors. Also, notice how we didn’t hardcode the name of the attribute in the second method below:
# This is too verbose
try:
some_setting = settings.SOME_SETTING
except AttributeError:
some_setting = DEFAULT_VALUE
# Plain and simple
some_setting = getattr(settings, 'SOME_SETTING', DEFAULT_VALUE)
Instead of requiring consumers to define all your app settings it is better to supply sensible defaults. Also I find it useful to prefix names of app settings within settings.py
# in settings.py
MYAPP_FOO_CHOICES = [('bar', u'Bar'), ('baz', u'Baz')]
# in myapp/models.py
from django.db import models
from django.conf import settings
FOO_CHOICES = getattr(settings, 'MYAPP_FOO_CHOICES', [('quux', u'Quux')])
class FooRecord(models.Model):
foo = models.CharField(max_length=10, choices=FOO_CHOICES)
This works fine for simple apps with fewer settings. But it can easily get out of hand when your app grows. An app_settings.py module would help keeping track of configuration by keeping all configuration options in one place:
# in myapp/app_settings.py
from django.conf import settings
FOO_CHOICES = getattr(settings, 'MYAPP_FOO_CHOICES', [('quux', u'Quux')])
# in myapp/models.py
from django.db import models
from app_settings import FOO_CHOICES
class FooRecord(models.Model):
foo = models.CharField(max_length=10, choices=FOO_CHOICES)
To summarize the points above:
- Import settings from django.conf
- Use getattr()
- Always supply a default value.
- Prefix settings you made up in settings.py
- Use app_settings.py if you have many
Next part is Developing Reusable Django Apps: Signals.
[1] | If your configuration is long, say more than 100 lines, you should step back and reconsider. Perhaps you should prefer a strategy similar to django.contrib.sitemaps or django.contrib.syndication. |
If you have any questions, suggestions or corrections feel free to drop me a line.