atb00ker
atb00ker

Reputation: 1105

Django: Change settings value during run-time in migrations

I am trying to change a value in settings.py during run-time while creating migrations.

settings.py:

...
magicVar = "initValue"

0002_init:

...
def change_magicVar(apps, schema_editor):
    settings.magicVar = "newValue"
...

operations = [
    migrations.RunPython(change_magicVar),
]
...

0003_changed_migrations:

...
def print_magicVar(apps, schema_editor):
   # Yay! It prints the correct value
   print (settings.magicVar) # prints: newValue
...

operations = [
   migrations.RunPython(print_magicVar),
   migrations.CreateModel(
     name='MagicModel',
        fields=[
            ('someMagic', 
               models.ForeignKey(
                  # Oops! This still has the old value, i.e *initValue*
                  # I want to achieve the *newValue* here!
                  default=settings.magicVar,
                  ... 
    )),

I want the changed value in migrations, but it looks like the value is already been cached. Does django provide an abstraction to refresh the migrations cache and put a new value in it? If not, what possible options do i have to achieve that value in the defaults?

Note: I am trying to avoid this solution because my database might give millions of records and iterating over them isn't ideal.

For external reasons i am also trying to avoid django-livesettings

Thank you!

Upvotes: 0

Views: 1232

Answers (1)

Vishvajit Pathak
Vishvajit Pathak

Reputation: 3731

You can not achieve it this way. You can check https://github.com/jazzband/django-constance .

Upvotes: 1

Related Questions