To create a model that only allows one instance, inherit from solo.models.SingletonModel.
Note: You do not need to provide verbose_name_plural in the Meta class; Django Solo uses the verbose_name instead.
If you are converting an existing model with data to a singleton, you can specify which row to use as the singleton by setting the singleton_instance_id property.
from django.db import models
from solo.models import SingletonModel
class SiteConfiguration(SingletonModel):
site_name = models.CharField(max_length=255, default='Site Name')
maintenance_mode = models.BooleanField(default=False)
def __str__(self):
return "Site Configuration"
class Meta:
verbose_name = "Site Configuration"
# To migrate an existing model with a specific ID:
class SiteConfiguration(SingletonModel):
singleton_instance_id = 24
# (...)