django-app-defaults

User’s Guide

Installation

Requirements

Python 2.7, +3.4
Django 1.8, 1.11, +2.0

Pip

pip install django-app-defaults

No configuration is necessary, just install and you are ready to go

Usage

Create settings

Settings must be defined within a defaults module at the root of the app:

# my_app/defaults.py

# `django.conf.settings` or any other
# module can be imported if needed

# required
DEFAULT_SETTINGS_MODULE = True

# define default settings below
MY_DEFAULT_SETTING = "yey"

Use settings

Then anywhere within your project:

from app_defaults import settings

print(settings.MY_DEFAULT_SETTING)
# yey

# All `django.conf.settings` are also available
print(settings.DEBUG)
# True

Load settings for a single app

Note: the DEFAULT_SETTINGS_MODULE variable is not required when explicitly passing apps or modules
from app_defaults import Settings

settings = Settings(apps=["my_app"])

# or

from my_app import defaults
settings = Settings(modules=[defaults])

API Reference

API

app_defaults module

app_defaults.settings app_defaults.Settings object

Get a setting from django settings or app defaults. In that order

Parameters:
  • apps (list) – List of apps to search for a defaults module. It’s usually a subset of the INSTALLED_APPS setting. The order matters
  • modules (list) – List of module objects to lookup for settings
  • locals_only (bool) – This is False by default, mainly because it only works when the app’s path is relative to the running script. It’s considered better to pass the list of local apps instead or make use of check_magic_var. This may be deprecated in the future
  • check_magic_var (bool) – This checks the magic var DEFAULT_SETTINGS_MODULE = True is present in each app defaults module. If the var is False or does not exists, then the module is ignored.

Settings Object

class app_defaults.Settings(apps=None, modules=None, locals_only=False, check_magic_var=False)

Get a setting from django settings or app defaults. In that order

Parameters:
  • apps (list) – List of apps to search for a defaults module. It’s usually a subset of the INSTALLED_APPS setting. The order matters
  • modules (list) – List of module objects to lookup for settings
  • locals_only (bool) – This is False by default, mainly because it only works when the app’s path is relative to the running script. It’s considered better to pass the list of local apps instead or make use of check_magic_var. This may be deprecated in the future
  • check_magic_var (bool) – This checks the magic var DEFAULT_SETTINGS_MODULE = True is present in each app defaults module. If the var is False or does not exists, then the module is ignored.

Additional Notes

Changelog

1.2

  • Support AppConfig

1.1

  • Docs

1.0

  • Initial release

License

The MIT License (MIT)

Copyright (c) 2018 Esteban Castro Borsani <ecastroborsani@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.