0

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.

2
  • 2
    Settings are meant to configure the application, not to provide data. They should be considered read-only. Commented Feb 21, 2024 at 22:33
  • 1
    Simplest way to do read-write "settings" is to define a model, even if that database table is only ever going to have one row. Perhaps only one field (JSONfield) as well. But if at any future time you want per-user settings or day-of-week specific settings or ... it's trivial. Commented Feb 22, 2024 at 10:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.