Search code examples
pythonwagtail

Wagtail, register settings singleton


from wagtail.contrib.settings.models import BaseGenericSetting, register_setting

@register_setting
class Glossary(BaseGenericSetting)
    ...

Is it possible to restrict max number of instances that can be created? Maybe there is some easy way or only overwriting save method


Solution

  • This is how BaseGenericSetting works as standard - it ensures that only a single instance of the setting object is ever created. If you visit the settings area in the admin again once it has been created, it will allow you to edit the existing object, not create a new one.

    This might only be enforced within the admin view rather than at the model level, though - if you need to work with settings objects in code, use Glossary.load() to retrieve the single instance. (This will create the instance if it doesn't already exist.)