Skip to main content

A Django app that provides an abstract model of a singleton object.

Project description

All By Myself - Django Singletons

Singletons are objects that can only be instantiated once and serve a specific purpose. A classic example of where a singleton is necessary in web development is site configuration. This package provides an abstract singleton base model, SingletonBaseModel, along with a model admin, SingletonBaseModelAdmin, both of which are utilized to create and manage singleton objects in Django.

Installation

$ pip install django-allbymyself

Quick Start

Add to INSTALLED_APPS in your project's settings.py to load custom admin templates:

INSTALLED_APPS = [
    ...
    'allbymyself',
]

Create a model in your_app/models.py and subclass SingletonBaseModel:

from django.db import models
from allbymyself.models import SingletonBaseModel

class SiteSettings(SingletonBaseModel):
    site_title = models.CharField(max_length=50)
    about = models.CharField(max_length=255)

Register the model in your_app/admin.py, subclassing SingletonBaseModelAdmin:

from django.contrib import admin
from allbymyself.admin import SingletonBaseModelAdmin
from your_app.models import SiteSettings

@admin.register(SiteSettings)
class SiteSettingsAdmin(SingletonBaseModelAdmin):
    fields = ('site_title', 'about')

Features

  • Skips change list page and instead goes straight to the change form or add form.
  • SingletonBaseModel handles caching and uncaching.
  • Admin URLs for change form and history form will not include object id.
  • After saving changes or adding a new instance, the admin user is redirected to the admin index.
  • Override is_default_available and return True to create an instance on admin page startup:
class SiteSettings(SingletonBaseModel):
    site_title = models.CharField(max_length=50, blank=True)
    about = models.CharField(max_length=255, blank=True)

    @classmethod
    def is_default_available(cls):
        # if True, make sure to handle field defaults in your model
        # appropriately!
        return True

Context Processor

You may also add your object as a context processor to make it available in all templates, site-wide. First create your_app/context_processors.py and add the following:

from django.urls import reverse
from your_app.models import SiteSettings

def site_settings(request):
    if request.path.startswith(reverse('admin:index')):
        return {}
    else:
        return {'site_settings': SiteSettings.get()}

The above if statement prevents creation of an instance on admin page startup. This is only necessary if is_default_available returns False. Then, in your project's settings.py:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'your_app.context_processors.site_settings',
            ],
        },
    },
]

You can then access your object in your templates like any other piece of context:

<h1>{{ site_settings.site_title }}</h1>
<p>{{ site_settings.about }}</p>

Testing

Simply run tests like so:

$ python manage.py test allbymyself

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-allbymyself-1.0.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

django_allbymyself-1.0.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file django-allbymyself-1.0.1.tar.gz.

File metadata

  • Download URL: django-allbymyself-1.0.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.2

File hashes

Hashes for django-allbymyself-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9d0d13904bf43e03c1df32b3a066fe38490534e192da96f9e2f49fb701dbd0f6
MD5 a532f0b0646216615ba82e41c70f9b9e
BLAKE2b-256 f57b4043a251ebe985555134a9d9473669c5452d27651e2ae10c998bb266890c

See more details on using hashes here.

File details

Details for the file django_allbymyself-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_allbymyself-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.2

File hashes

Hashes for django_allbymyself-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5a121a224a33a587711594849eada9d31b10c2c9f65435a75924a367268e07d3
MD5 34027de1252a0efddb91fb2aa8049909
BLAKE2b-256 0f381e852b89c5d7c1f7d516d5abfab58ac430df77010b5d10da0ab6023ffebd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page