Skip to main content

A Django app to enforce the use of only one instance for a given model.

Project description

Django One Instance

build-status-image pypi-version python-version django-version

Django One Instance is a Django app which enforces the use of a single entry for a given model (i.e.: singleton model).

The app provides an abstract model to extend which enforces the singleton models behaviour and an admin base class for registering the singleton models in the admin site.

Usage

Say you have a Config model and you want to enforce its use only with one instance. All you need to to is to extend the SingletonModel abstract model which provides a custom manager for this purpose.

# models.py
from one_instance.models import SingletonModel

class Config(SingletonModel):

    enabled = models.BooleanField()

You can also register the model in the django admin and it will be aware that there is only one object.

# admin.py
from django.contrib import admin

from testapp.models import Config
from one_instance.admin import SingletonAdmin


admin.site.register(Config, SingletonAdmin)

New singleton model

from testapp.models import Config
>>> Config.objects.create(enabled=False)
<Config: Config object (1)>
>>> Config.objects.get()
<Config: Config object (1)>

Note how you don't have to pass the pk to the get() method. If you try to create another instance you get an error.

>>> Config.objects.create(enabled=False)
one_instance.models.SingletonModelAlreadyExists: You are receiving this error after attempting to create another instance of the singleton model. Set DJANGO_ONE_STRICT=False to drop the exception and return the model instance instead.

Pre-existing model

If you extend the SingletonModel for a pre-existing model with many instances, the default get() behaviour is to return the last entry which becames the singleton object.

>>> Config.objects.get()
<Config: Config object (3)>

the other entries are hidden by the manager

>>> Config.objects.all()
<SingletonModelQuerySet [<Config: Config object (3)>]>
>>> Config.objects.first()
<Config: Config object (3)>
>>> Config.objects.last()
<Config: Config object (3)>

if you try to forcefully get one of the other instances, you get an error

>>> Config.objects.get(pk=1)
TypeError: You should use get() method without any arguments. Set DJANGO_ONE_STRICT=False if you want to silently drop the unneeded arguments.

Installation

  • pip install django-one-instance
  • Add "one_instance" to your INSTALLED_APPS setting like this:
    INSTALLED_APPS = [
        ...,
        "one_instance",
    ]
    

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-one-instance-0.0.2.tar.gz (6.0 kB view hashes)

Uploaded Source

Built Distribution

django_one_instance-0.0.2-py3-none-any.whl (7.6 kB view hashes)

Uploaded Python 3

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