A Django app to enforce the use of only one instance for a given model.
Project description
Django One Instance
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-one-instance-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d19d1395e9978904fc617233f2ff7de354a5c3d0c555f4027d3540597ea272e7 |
|
MD5 | 9ed8ef263c80e8f02918243b196de3e8 |
|
BLAKE2b-256 | e592b9579ff96c6ef50ce76d260b2f733bb9383a4869f8fd264371505bcc3cd1 |
Hashes for django_one_instance-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75cb9ed105be4168e26dc6ed923dc9afbc124d164c22a9a309bf4518a6a9a7d7 |
|
MD5 | 8d56785df0b40db0cea4d4e1eed0a05f |
|
BLAKE2b-256 | eef748ab0ab2a26ddc70caec003122c1b91e45e8a1a5e1a2b225ff8bb7d3004f |