Package: django-modelsettings
django-modelsettingsThis Django application allows to define Django application settings with Django ORM models and edit them in the admin area.Please check the other alternatives athttps://www.djangopackages.com/grids/g/live-setting/.
This project started because I tried them all and I was not really happy with any of them.Key pointsThe settings are stored in the database, in normalized tables with proper field types and constraints. In particular, it is possible to have a setting which is a ForeignKey to another model (e.g. “Default account”), and it will properly validate and/or update when the corresponding model is deleted.The settings are described using standard Django ORM classes (likeEmailFieldorSmallPositiveIntegerField) with all the bells and whistles (e.g. validators). There is no parallel hierarchy of classes for different types of data.Each application may have any number of settings groups, however there is no fancy and complex syntax for that. The developer may simply add moreSettingsclasses if he decides so.The settings are lazy and effective. The database is not hit until the settings are actually accessed, and then it takes exactly one SQL query to fetch all settings for all apps.The system behaves correctly when there is no corresponding data in the database (if the settings have never been saved yet, or have been saved partly).Programmatically, all settings from all application are accessible via a single object.There is a page for Django Admin.RequirementsThe latest version supports Django 1.7-1.9.Installationpip install django-modelsettingsMigration from 0.1.xdjango-modelsettings 0.1.x had a different API and supported Django 1.4-1.8. To migrate from 0.1.x, runmanage.py migrate dbsettings--fake.UsageAdddbsettingstoINSTALLED_APPS:# settings.pyINSTALLED_APPS=[...'dbsettings',]AddSettingsclass in your application:# blog/models.pyfromdbsettings.modelsimportAppSettingsclassSettings(AppSettings):contact_email=models.EmailField(default="info@localhost")update_interval=models.PositiveIntegerField(null=True,default=10,help_text="Update interval in seconds")facebook_app_id=models.CharField("Facebook App ID",max_length=32,blank=True)Create the corresponding database tables:./manage.pymakemigrations&&./manage.pymigrateIn your business logic code, access settings viadjango.conf.settings.db:fromdjango.confimportsettingsprint(settings.db.blog.contact_email)settings.db.blog.update_interval=60settings.db.blog.save()or directly:fromdbsettingsimportsettingsprint(settings.blog.contact_email)settings.blog.update_interval=60settings.blog.save()print(settings.django.SECRET_KEY)# shortcut to django.conf.settingsAdmin areaThe settings editor will be automatically added at Django Admin > Settings.You can also add a direct link (e.g. in youradmin/base_site.htmloverrides):<a href="{%url'admin:dbsettings_root_changelist'%}">{%trans"Settings"%}</a>Customizing admin area formTo provide a custom admin form for your settings model, create aModelAdminclass and register it:# blog/admin.pyfromblog.modelsimportSettingsfromdbsettings.adminimportRootSettingsAdmin@RootSettingsAdmin.register(Settings)classSettingsAdmin(admin.ModelAdmin):defformfield_for_dbfield(self,db_field,**kwargs):ifdb_field.name=='welcome_text':kwargs['widget']=SummernoteWidget()returnsuper().formfield_for_dbfield(db_field,**kwargs)Several groups of settings per applicationIt is possible to split settings into several groups within one application.# blog/models.pyfromdbsettings.modelsimportAppSettingsclassSettings(AppSettings):option1=models.IntegerField()classFoo(AppSettings):option2=models.IntegerField()classBar(AppSettings):option3=models.IntegerField()fromdbsettingsimportsettingsprint(settings.blog.option1)print(settings.blog_foo.option2)print(settings.blog_bar.option3)