Change settings value during run-time in migrations

40 views
Skip to first unread message

Ajay Tripathi

unread,
Sep 25, 2018, 7:06:00 AM9/25/18
to Django users
Hi,

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[3] 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[2]

Thank You
Ajay Tripathi

P.S: Find formatted version of the question on stack overflow here[1].

---

Jason

unread,
Sep 25, 2018, 12:15:05 PM9/25/18
to Django users
You can't do it this way, unfortunately.  I would suggest you use https://github.com/jazzband/django-constance instead

Ajay Tripathi

unread,
Sep 27, 2018, 10:35:36 AM9/27/18
to Django users

Hi Jason,

You can't do it this way, unfortunately.  I would suggest you use https://github.com/jazzband/django-constance instead

Thanks, It helped me.

Cordially,
Ajay Tripathi (atb00ker)
Reply all
Reply to author
Forward
0 new messages