29

I would like to control some configuration settings for my project using a database model. For example:

class JuicerBaseSettings(models.Model):
    max_rpm = model.IntegerField(default=10)
    min_rpm = model.IntegerField(default=0)

There should only be one instance of this model:

juicer_base = JuicerBaseSettings()
juicer_base.save()

Of course, if someone accidentally creates a new instances, it's not the end of the world. I could just do JuicerBaseSettings.objects.all().first(). However, is there a way to lock it down such that it's impossible to create more than 1 instance?

I found two related questions on SO. This answer suggests using 3rd party apps like django-singletons, which doesn't seem to be actively maintained (last update to the git repo is 5 years ago). Another answer suggests using a combination of either permissions or OneToOneField. Both answers are from 2010-2011.

Given that Django has changed a lot since then, are there any standard ways to solve this problem? Or should I just use .first() and accept that there may be duplicates?

6
  • 1
    ... or you could do .get(pk=1) instead...
    – Bakuriu
    Commented Sep 9, 2016 at 13:47
  • 3
    this shouldn't be in the database at all
    – e4c5
    Commented Sep 9, 2016 at 13:52
  • 4
    @e4c5 reasonable, but in small projects I always face with site settings like that, which allow to client change in admin, something like phone number or other related to project stuff. Maybe you point to better way Commented Sep 9, 2016 at 13:58
  • 2
    @e4c5 Could you recommend an alternative? One benefit of having this in the database means I can expose it via a REST API. Commented Sep 9, 2016 at 14:09
  • django-constance does exactly what you need
    – maxbellec
    Commented Nov 12, 2020 at 13:16

7 Answers 7

37

You can override save method to control a number of instances:

class JuicerBaseSettings(models.Model):

    def save(self, *args, **kwargs):
        if not self.pk and JuicerBaseSettings.objects.exists():
        # if you don't check for the existence of self.pk, you'll get 
        # an error when updating the current instance as well
            raise ValidationError('There can be only one JuicerBaseSettings instance')
        return super(JuicerBaseSettings, self).save(*args, **kwargs)
13

Either you can override save and create a class function JuicerBaseSettings.object()

class JuicerBaseSettings(models.Model):

    @classmethod
    def object(cls):
        return cls._default_manager.all().first() # Since only one item

    def save(self, *args, **kwargs):
        self.pk = self.id = 1
        return super().save(*args, **kwargs)

============= OR =============

Simply, Use django_solo.

https://github.com/lazybird/django-solo

Snippet Courtsy: django-solo-documentation.

# models.py


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 __unicode__(self):
        return u"Site Configuration"

    class Meta:
        verbose_name = "Site Configuration"
# admin.py

from django.contrib import admin
from solo.admin import SingletonModelAdmin
from config.models import SiteConfiguration

admin.site.register(SiteConfiguration, SingletonModelAdmin)

# 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()
1
  • 5
    for django_solo that do it in clean way
    – Bheid
    Commented Nov 27, 2019 at 8:02
13

If your model is used in django-admin only, you additionally can set dynamic add permission for your model:

# some imports here
from django.contrib import admin
from myapp import models

@admin.register(models.ExampleModel)
class ExampleModelAdmin(admin.ModelAdmin):
    
    # some code...

    def has_add_permission(self, request):
        # check if generally has add permission
        permitted = super().has_add_permission(request)
        # set add permission to False, if object already exists
        if permitted and models.ExampleModel.objects.exists():
            permitted = False
        return permitted 
3
  • 2
    This is great and efficient. However, the super needs the class name and self as argument (super(ExampleModelAdmin, self).has_add_permission(request)
    – Peterpanx
    Commented Sep 13, 2018 at 17:36
  • 2
    @Peterpanx not with Python 3 Commented Feb 5, 2022 at 22:51
  • 1
    This worked on django 5.0!! Overriding save method in model didn't work. Commented Jun 1 at 10:57
4

i am not an expert but i guess you can overwrite the model's save() method so that it will check if there has already been a instance , if so the save() method will just return , otherwise it will call the super().save()

1
  • Yes, I agree this is the best answer. Overwrite save() and check if it has a pk. If it does, then it is an update and that is fine, if not... Then check the dB to see if id=1 exists and handle accordingly. Commented Sep 9, 2016 at 14:01
3

You could use a pre_save signal

@receiver(pre_save, sender=JuicerBaseSettings)
def check_no_conflicting_juicer(sender, instance, *args, **kwargs):
    # If another JuicerBaseSettings object exists a ValidationError will be raised
    if JuicerBaseSettings.objects.exclude(pk=instance.pk).exists():
        raise ValidationError('A JuiceBaseSettings object already exists')
0
1

I'm a bit late to the party but if you want to ensure that only one instance of an object is created, an alternative solution to modifying a models save() function would be to always specify an ID of 1 when creating an instance - that way, if an instance already exists, an integrity error will be raised. e.g.

JuicerBaseSettings.objects.create(id=1)

instead of:

JuicerBaseSettings.objects.create()

It's not as clean of a solution as modifying the save function but it still does the trick.

1

I did something like this in my admin so that I won't ever go to original add_new view at all unless there's no object already present:

def add_view(self, request, form_url='', extra_context=None):
    obj = MyModel.objects.all().first()
    if obj:
        return self.change_view(request, object_id=str(obj.id) if obj else None)
    else:
        return super(type(self), self).add_view(request, form_url, extra_context)

def changelist_view(self, request, extra_context=None):
    return self.add_view(request)

Works only when saving from admin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.