Package: django-solo
Django Solo+---------------------------+
|                           |
|                           |
|             \             | Django Solo helps working with singletons:
|             /\            | database tables that only have one row.
|           >=)'>           | Singletons are useful for things like global
|             \/            | settings that you want to edit from the admin
|             /             | instead of having them in Django settings.py.
|                           | 
|                           | 
+---------------------------+FeaturesSolo helps you enforce instantiating only one instance of a model in django.You define the model that will hold your singleton object.django-solo gives helper parent class for your model and the admin classes.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.Use CasesDjango 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 settingsAn image slider that has many slidesA page section that has sub-sectionsA team bio with many team membersThere are many cases where it makes sense for the parent in a one to many relationship to be limited to a single instance.Usage Example# models.pyfromdjango.dbimportmodelsfromsolo.modelsimportSingletonModelclassSiteConfiguration(SingletonModel):site_name=models.CharField(max_length=255,default='Site Name')maintenance_mode=models.BooleanField(default=False)def__str__(self):return"Site Configuration"classMeta:verbose_name="Site Configuration"# admin.pyfromdjango.contribimportadminfromsolo.adminimportSingletonModelAdminfromconfig.modelsimportSiteConfigurationadmin.site.register(SiteConfiguration,SingletonModelAdmin)# There is only one item in the table, you can get it this way:from.modelsimportSiteConfigurationconfig=SiteConfiguration.objects.get()# get_solo will create the item if it does not already existconfig=SiteConfiguration.get_solo()In your model, note how you did not have to provide averbose_name_pluralfield -
That's because Django Solo uses theverbose_nameinstead.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 settingsingleton_instance_idproperty on the model:classSiteConfiguration(SingletonModel):singleton_instance_id=24# (...)InstallationThis application requires Django 3.2, 4.0, or 4.1.Install the package usingpip install django-soloAddsoloorsolo.apps.SoloAppConfigto yourINSTALLED_APPSsetting.This is how you run tests:./manage.py test solo --settings=solo.tests.settingsAnd from withintox:python -m pip install tox
toxSupported LanguagesEnglishSpanishGermanAdminThe standard Django admin does not fit well when working with singleton,
for instance, if you need some global site settings to be edited in the admin.
Django Solo provides a modified admin for that.In the admin home page where all applications are listed, we have aconfigapplication that holds a singleton model for site configuration.The configuration object can only be changed, there's no link for "add" (1).The link to the configuration page (2) directly goes to the form page - no
need for an intermediary object list page, since there's only one object.The edit page has a modified breadcrumb (3) to avoid linking to the
intermediary object list page.From the edit page, we cannot delete the object (4) nor can we add a new one (5).If you wish to disable the skipping of the object list page, and have the default
breadcrumbs, you should setSOLO_ADMIN_SKIP_OBJECT_LIST_PAGEtoFalsein your settings.Availability from templatesThe singleton object can be retrieved from template by giving the Django model
dotted path:{%get_solo'app_label.ModelName'asmy_config%}Example:{%loadsolo_tags%}{%get_solo'config.SiteConfiguration'assite_config%}{{site_config.site_name}}{{site_config.maintenance_mode}}If you're extending a template, be sure to use the tag in the proper scope.Right:{%extends"index.html"%}{%loadsolo_tags%}{%blockcontent%}{%get_solo'config.SiteConfiguration'assite_config%}{{site_config.site_name}}{%endblockcontent%}Wrong:{%extends"index.html"%}{%loadsolo_tags%}{%get_solo'config.SiteConfiguration'assite_config%}{%blockcontent%}{{site_config.site_name}}{%endblockcontent%}CachingBy default caching is disabled: every timeget_soloretrieves the singleton
object, there will be a database query.You can enable caching to only query the database when initially retrieving the
object. The cache will also be updated when updates are made from the admin.The cache timeout is controlled via theSOLO_CACHE_TIMEOUTsettings.
The cache backend to be used is controlled via theSOLO_CACHEsettings.SettingsTemplate tag nameYou can retrieve your singleton object in templates using theget_solotemplate tag.You can change the nameget_solousing theGET_SOLO_TEMPLATE_TAG_NAMEsetting.GET_SOLO_TEMPLATE_TAG_NAME='get_config'Admin override flagBy default, the admin is overridden. But if you wish to keep the object list
page (e.g. to customize actions), you can set theSOLO_ADMIN_SKIP_OBJECT_LIST_PAGEtoFalse.SOLO_ADMIN_SKIP_OBJECT_LIST_PAGE=TrueCache backendDjango provides a way to define multiple cache backends with theCACHESsettings. If you want the singleton object to be cached separately, you
could define theCACHESand theSOLO_CACHEsettings like this:CACHES={'default':{'BACKEND':'django.core.cache.backends.memcached.MemcachedCache','LOCATION':'127.0.0.1:11211',},'local':{'BACKEND':'django.core.cache.backends.locmem.LocMemCache',},}SOLO_CACHE='local'Caching will be disabled if set toNone.Cache timeoutThe cache timeout in seconds.SOLO_CACHE_TIMEOUT=60*5# 5 minsCache prefixThe prefix to use for the cache key.SOLO_CACHE_PREFIX='solo'Getting the codeThe code is hosted athttps://github.com/lazybird/django-solo/Check out the latest development version anonymously with:$ git clone git://github.com/lazybird/django-solo.gitYou can install the package in the "editable" mode like this:pip uninstall django-solo  # just in case...
pip install -e git+https://github.com/lazybird/django-solo.git#egg=django-soloYou can also install a specific branch:pip install -e git+https://github.com/lazybird/django-solo.git@my-branch#egg=django-soloThe package is now installed in your project and you can find the code.To run the unit tests:pip install tox
toxMaking a releaseUpdatesolo/__init__.pyversionMake a new release on GitHubtox-ebuildtox-eupload