Skip to main content

Yet Another App Settings - manage settings for your reusable app

https://travis-ci.org/radiac/django-yaa-settings.svg?branch=master https://coveralls.io/repos/github/radiac/django-yaa-settings/badge.svg?branch=master

Features

  • Easy to install and use

  • Provide defaults and validate user settings before use

  • Works with Django’s settings overrides in tests

Supports Django 2.2 to 3.1 on Python 3.5 to 3.8.

Version 1.0.0 was the last version to support earlier versions of Django.

Installation

Install from pip:

pip install django-yaa-settings

Usage

In your app, create a local app_settings.py:

from yaa_settings import AppSettings

class MySettings(AppSettings):
    prefix = 'MYAPP'

    # Can be overridden in Django settings as MYAPP_ATTRIBUTE
    ATTRIBUTE = 'a static value defined at class creation'

    @property
    def PROPERTY(self):
        """
        Return a value calculated whenever it is accessed

        Can be overridden in Django settings as MYAPP_PROPERTY
        """
        return 'a value calculated when accessed'

    def CALLABLE(self, value):
        """
        Always called, passed the MYAPP_CALLABLE value from Django settings
        (or passed None if that is not defined)
        """
        if value is None:
            raise ValueError('MYAPP_CALLABLE must be configured')
        return value
  • Import and subclass AppSettings

  • Only define one AppSettings subclass per file

  • Set the prefix attribute if you want to give your settings a prefix in Django’s settings.

  • Settings should be uppercase for consistency with main Django settings.

Now you can access your app’s settings directly on the class, without the prefix:

from . import app_settings

def some_method(request):
    if app_settings.ATTRIBUTE == 1:
        return app_settings.PROPERTY
    return app_settings.CALLABLE

You can override these settings in your main Django settings, using the prefix:

MYAPP_ATTRIBUTE = 'value available as app_settings.ATTRIBUTE'
MYAPP_PROPERTY = 'value available as app_settings.PROPERTY'
MYAPP_CALLABLE = 'value passed to MySettings.CALLABLE'

Why?

Use the prefix to namespace your settings

It’s always a good idea to namespace your settings based on your app’s name to avoid collisions with other apps. By using the prefix attribute you can omit the prefix throughout your app, making your code neater.

The prefix is optional and you can manually namespace your settings if you’d prefer the consistency of using the same full setting throughout your project.

Namespace settings while retaining test support

Using the prefix mimicks the simpler settings.py that you find in some projects:

from django.conf import settings
SETTING = getattr(settings, 'MYAPP_SETTINGS', 'default')

but unlike that simpler pattern, Yaa-Settings still works with standard setting overrides for tests - see the Django documentation for more details.

Create dynamic defaults using properties

A property on your AppSettings subclass will be evaluated every time you access it, unless you override it in Django’s settings. This allows you to generate dynamic defaults at runtime.

Validate or standardise settings using methods

A method on your AppSettings subclass will be called every time you access it, and will be passed the value you have defined in Django’s settings. This allows you to validate settings, or process them ready for use.

Changelog

  • 1.1.0, 2020-07-22: Moved supported Django to 2.2 to 3.1 (#1, #2 - thanks to clinoge)

  • 1.0.0, 2018-06-24: Released as stable

  • 0.1.0, 2018-06-24: Initial release

Metadata

Release files for django-yaa-settings 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-yaa-settings 1.1.0
File Size Uploaded Provenance Actions
django-yaa-settings-1.1.0.tar.gz 8.0 kB Without provenance Details for django-yaa-settings-1.1.0.tar.gz

Release files / django-yaa-settings-1.1.0.tar.gz

Download URL django-yaa-settings-1.1.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7cd3e7fdb4cf632394dd3872734bb92dd3de4394ddc760fa8a2d33cc645e4971
BLAKE2b-256 checksum
How to use checksums
3e62215259ab49a5c4877b6c966c10a7270f93a3501e3f5aeed034d823d03c30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.0

Security for django-yaa-settings 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page