Confusing Django settings content using manage.py vs call_command

25 Views Asked by At

I have a Django command which I can run using manage.py. The command imports django.conf.settings, then I grab Songs from the database to update bits of the settings:

from django.conf import settings
from music.models import Song


class Command(BaseCommand):

  def handle(self, **options):
    new_songs = SongSerializer(Song.objects.all(), many=True).data  
    settings.SONGS_TO_EXPORT = new_songs
    print(settings.SONGS_TO_EXPORT)
    # ...

The confusing behaviour is:

  • When I run the command using manage.py, I can confirm I get a fresh copy of the settings and the UPDATED content of settings.SONGS_TO_EXPORT is printed out.

  • When I run the command in another app using Django's call_command, I get an old (perhaps cached???) version of the settings and the content of settings.SONGS_TO_EXPORT is just old. Even if I run the command with manage.py first, I still get a non-updated version of the settings in the call_command version.

My aim is to get the same results with call_command.

0

There are 0 best solutions below