Tuesday, September 25, 2018

Change settings value during run-time in migrations

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].

---

[1]: https://stackoverflow.com/questions/52459208/django-change-settings-value-during-run-time-in-migrations
[2]: https://django-livesettings.readthedocs.io/en/latest/about.html
[3]: https://stackoverflow.com/a/40601025/6410464

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ec7e71ac-52c7-459c-9ee9-3b3da64f3fff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment