I would like to control some configuration options for my project using the database model. For instance:
class JuicerBaseSettings(models.Model):
max_rpm = model.IntegerField(default=10)
min_rpm = model.IntegerField(default=0)
There should be only one instance of this model:
juicer_base = JuicerBaseSettings()
juicer_base.save()
Of course, if someone accidentally creates new instances, this is not the end of the world. I could just do it JuicerBaseSettings.objects.all().first(). However, is there a way to lock it in such a way that it is impossible to create more than one instance?
I found two related questions about SO. This answer suggests using third-party applications, such as django-singletonsone that doesn't seem to be supported (last update for the git repository 5 years ago). Another answer suggests using a combination of permissions or OneToOneField. Both answers relate to 2010-2011.
Given that Django has changed a lot since then, are there standard ways to solve this problem? Or should I just use .first()and acknowledge that there may be duplicates?
source
share