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. Alternatively, you can explicitly provide the pk of the instance to use with the Meta option singleton_pk.

class PreExistingModelExample(SingletonModel):

    class Meta:

        singleton_pk = 2
>>> PreExistingModelExample.objects.get()
<PreExistingModelExample: PreExistingModelExample object (2)>

the other entries are hidden by the manager

>>> PreExistingModelExample.objects.all()
<SingletonModelQuerySet [<PreExistingModelExample: PreExistingModelExample object (2)>]>
>>> PreExistingModelExample.objects.first()
<PreExistingModelExample: PreExistingModelExample object (2)>
>>> PreExistingModelExample.objects.last()
<PreExistingModelExample: PreExistingModelExample object (2)>

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

>>> PreExistingModelExample.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.

Model inheritance

The objects manager of the singleton model will include the custom methods from each objects manager of each parent class (if any).

class ManagerA(models.Manager):

    def as_json(self):
        return serializers.serialize("json", self.all())

class ExtraManagerA(models.Model):

    objects = ManagerA()

    class Meta:
        abstract = True

class ModelInheritanceExample(SingletonModel, ExtraManagerA):

    pass
>>> models.ModelInheritanceExample.objects.get()
<ModelInheritanceExample: ModelInheritanceExample object (1)>
>>> models.ModelInheritanceExample.objects.as_json()
'[{"model": "testapp.modelinheritanceexample", "pk": 1, "fields": {}}]'

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.4.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_one_instance-0.0.4-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file django-one-instance-0.0.4.tar.gz.

File metadata

  • Download URL: django-one-instance-0.0.4.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for django-one-instance-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a74055cb4f09d0a9a04a172a871814c1744f9dc52bd5564c6dd110fb54d4502b
MD5 20cdf16a961ec6a2d30bd8c2a17ee469
BLAKE2b-256 b43075c1112fa6d72eb00a916bcff584f63e3fba34a50b9386af90654761aba8

See more details on using hashes here.

File details

Details for the file django_one_instance-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_one_instance-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 063690942e28a90a69788c99a853929a6a49ba0cf580cd3d933ff18590e0cc62
MD5 560bbdf2316aa236a19007695d2fb03d
BLAKE2b-256 2c52ec0d0eceb3f5d60a5baf09e5cedb2bf6bb005046bdb2988f8aca8ce83bba

See more details on using hashes here.

Supported by

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