Issue #26 open

Access settings from model

Anonymous created an issue

I want to access some values directly from models.py, there are not error, but it returns the default one instead the one that is saved in the db? I'm missing something? Thanks

Comments (3)

  1. Hynek Cernoch

    Reading livesettings values immediately from models.py is a bad idea for several reasons. Assigning livesetting value wrapped in lazy value is OK in models.py:

    • The class livesettings.models.Setting can not be requested to access database before all models.py listed in INSTALLED_APPLICATIONS are completely imported. (chicken and egg problem) You can use it only as some lazy value which is assigned in models and evaluated.
    • It is a good idea that values to be saved into livesettings should be only values which can be modified at runtime, e.g. model choices, not values which would require python server restart to be applied. (This is a good convence important for users. Values which have been copied to several places in the meantime need a restart whereas values assigned as a function can be live modified. It justifies why livesettings does not try to access database directly and why it waits for django.db.)

    A frequent example are models choices, which are supported Directly in django.db.models as lazy values. They can be assigned as iterator or function, but verifying its values is skipped initially in the model verifying and they are evaluated for the first time e.g. when a form instance is created for that model. Then they stay cached in the memory in django.db and that iterator or function is never called or evaluated second time.

    Models are imported before every manage.py command, even before syncdb, the command which is used to create all tables including livesettings_setting itself. Only in the case "DatabaseError table ... doesnotexist" should db values be replaced by default values silently, only with a warning in the log. (TODO This is to be verified. You need livesettings 1.4.8 or newer)

    Read the last paragraph about choices in http://docs.djangoproject.com/en/1.3/ref/models/fields/#choices

    TODO: Better explaining of that all should be added to the livesettings documentation.

  2. Log in to comment