| \ | Django Solo helps working with
| /\ | database tables that only have one row.
| >=)'> | Singletons are useful for things like
| \/ | settings that you want to edit from the
| / | instead of having them in Django
Solo helps you enforce instantiating only one instance of a model in
* You define the model that will hold your singleton object.
* django-solo gives helper parent class for your model and the admin
* You get an admin interface that's aware you only have one object.
* You can retrieve the object from templates.
* By enabling caching, the database is not queried intensively.
Django Solo is also great for use with singleton objects that have a one to
many relationship. Like the use case below where you have a 'Home Slider"
that has many "Slides".
* Global or default settings
* An image slider that has many slides
* A page section that has sub-sections
* A team bio with many team members
There are many cases where it makes sense for the parent in a one to many
relationship to be limited to a single instance.
from django.db import models
from solo.models import SingletonModel
site_name = models.CharField(max_length=255, default='Site Name')
maintenance_mode = models.BooleanField(default=False)
return "Site Configuration"
verbose_name = "Site Configuration"
from django.contrib import admin
from solo.admin import SingletonModelAdmin
from config.models import SiteConfiguration
# There is only one item in the table, you can get it this way:
from .models import SiteConfiguration
config = SiteConfiguration.objects.get()
# get_solo will create the item if it does not already exist
config = SiteConfiguration.get_solo()
In your model, note how you did not have to provide a `verbose_name_plural`
That's because Django Solo uses the `verbose_name` instead.
If you're changing an existing model (which already has some objects stored
in the database) to a singleton model, you can explicitly provide the id of
the row in the database for django-solo to use. This can be done by setting
`singleton_instance_id` property on the model:
singleton_instance_id = 24
Configuration Switches (platform-specific settings discarded)
PY310 OFF Build using Python 3.10
PY39 ON Build using Python 3.9
Package Dependencies by Type
Distribution File Information
9046eca738f2ed64dbef38c2107a02af1065a8899b4f9fabf61b06b8325de1b4 13530 django_solo-2.0.0-py3-none-any.whl
Ports that require python-django-solo:py39
|No other ports depend on this one.|