Skip to main content

Django Preferences Utils

Version Build status License Downloads

Screenshot

All you need to manage your django project preferences.

Motivation

So you need to manage your django project preferences.
You tried many different solutions bu no one seemed to work well.
And then you came across this one.
Let's talk about it.

There is nothing special about managing preferences in Django. A simple model is everything you need.
The problem is that you just need one record, isn't it?
And you don't want to have an ugly changelist view with just one row.
Nothing special, you can solve it, but it's boring.

So here comes Django Preferences Utils.

You define your preferences in a model, use everything you want: file fields, image fields, json fields, translations, everything. Just make sure to subclass what you have to as explained below, register your model and you're done. All the other stuff is taken care of by Django Preferences Utils.

Oh, and you can also define multiple models for your preferences.

How to

Install Django Preferences Utils

$ pip install django-preferences-utils

Add it to your INSTALLED_APPS:

    INSTALLED_APPS = (
        ...
        'django_preferences_utils',
        ...
    )

Create your preferences model:

from django.db import models
from preferences_utils.models import PreferencesUtilsModel

# "pref" is the name used internally to ref this particular model
# and is also the key used by the context processor and accessible in all your templates
@PreferencesUtilsModel.register("pref")
class Preferences(PreferencesUtilsModel):
    site_title = models.CharField(_('site title'), max_length=100, default="The best site in the world", blank=True)

    class Meta:
        verbose_name = _("site settings")

Important: Django Preferences Utils will save your model by itself when requiring a preferences instance. So be sure to have only nullable fields or fields with a default value.

Now probably you want to register you model in the admin:

from django.contrib import admin
from preferences_utils.admin import PreferencesUtilsAdmin
from .models import Preferences

@admin.register(Preferences)
class PreferencesNewAdmin(PreferencesUtilsAdmin):
    list_display = ('id', 'site_title', ) # whatever, is never shown

    fieldsets = (
        (_('Main'), {
            'fields': ("site_title",),
            "classes": ("baton-tabs-init",), # django-baton, you should use it ;)
        }),
    )

And propbably you need to use your preferences...

Inside a view:

from django.views.generic import View
from .models import Preferences

class MyView(View):
    pref = Preferences.instance()
    # or PreferencesUtilsModel.instance("pref")
    # ...

Or inside a template, in this case add to your context processors in your settings:

TEMPLATES = [
    # ...
    "OPTIONS": {
        # ...
        "context_processors": [
            # ...
            # notice the "pref" part at the end, this is the name used when registering your model
            "preferences_utils.context_processors.preferences.pref",
        ],
    }
]

And now you can use your preferences in your templates.

    <h1>{{ pref.site_title }}</h1>

How it works

Django Preferences Utils provides a few things:

  • A model to subclass which assures you have only one instance in your database
  • The same model class provides a class method to retrieve the instance
  • Registering your model to Django Preferences Utils allows you to have multiple preferences models and customize the name used in the context
  • An admin model to subclass which provides a custom changelist view
  • A context processor to retrieve your preferences

Running Tests

Does the code actually work?

$ source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_test.txt
(myenv) $ python runtests.py

Development commands

pip install -r requirements_dev.txt
invoke -l

Credits

Django Preferences Utils is developed by Otto SRL.

History

0.1.0 (2024-06-14)

  • First release on PyPI.

Metadata

Release files for django-preferences-utils 0.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-preferences-utils 0.1.0
File Size Uploaded
django_preferences_utils-0.1.0.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-preferences-utils 0.1.0
File Interpreter ABI Platform
django_preferences_utils-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 17.4 kB

Release files / django_preferences_utils-0.1.0.tar.gz

Download URL django_preferences_utils-0.1.0.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4060b626e417377c3d39d9b0220583854edd1bfc4a7c6160500b6d8a6bccb24e
BLAKE2b-256 checksum
How to use checksums
9671c0fa00a46131235b0659f23138f1423fb5c9d661b9877c10cbdbbe99bdc6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release files / django_preferences_utils-0.1.0-py2.py3-none-any.whl

Download URL django_preferences_utils-0.1.0-py2.py3-none-any.whl
Size 8.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5e52b9813ba0e3b039977730188b17ea5fd6425845c7c006da27fcc533dd81a4
BLAKE2b-256 checksum
How to use checksums
a6659d989dba9e5bb24ef1dad06933ed17c41002130473de96be09597d03813f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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