mikhail
mikhail

Reputation: 522

What is best way to store and edit global variables in django admin?

What is best way to store and edit global variables in django admin, something like what below or there some other faster or better ways to do it? models.py:

from django.db import models

class Section(models.Model):
    percent = models.PositiveSmallIntegerField(u'Percent')
    some_variable = models.PositiveSmallIntegerField(u'Some_variable')

admin.py:

from app.models import Settings

class Settings(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('percent', 'some_variable')
        }),
    )

And context processor like this:

from app.models import Settings

def settings(request):

    settings = Settings.objects.all()[0]

    from django.conf import settings
    return {
        'PERCENT':     settings.percent,
        'SOME_VARIABLE': settings.some_variable,
    }

Upvotes: 1

Views: 1923

Answers (2)

Victor Kotseruba
Victor Kotseruba

Reputation: 866

I have an app for this task, check it at https://github.com/barbuza/dbconfig

The only difference from your way is that I suggest to describe configuration sections as forms instead of models

It also have cache support, so your code will not touch database on each read operation on config values

Upvotes: 2

Nix
Nix

Reputation: 58542

Its hard to determine the "best way" without all the details, but your solution would work and might meet your needs. A couple draw backs are:

  1. You can only handle percents
  2. Every time you read a setting it is going to require a database call

I always recommend trying to find something before inventing it and there is a new app out there that does something simliar in a more generic fashion. django-setman (not a fan of the name. And there is a more experienced app called live-settings which I have played around with as well.

Upvotes: 0

Related Questions