Search code examples
pythondjangodjango-settings

Using variables as dict keys in django project settings


Is it safe and correct to use variables as dict keys in django project settings.py ? The chance for the names to be changed is small but it can happen in future.

For example, I have something like that now:

EXAMPLE_IDS = {
    'name1': 1,
    'name2': 2,
    'name3': 3,
}

I want to change it for something like this so I could use the names independently:

EXAMPLE_NAME_1 = 'name1'
EXAMPLE_NAME_2 = 'name2'
EXAMPLE_NAME_3 = 'name3'

EXAMPLE_IDS = {
    EXAMPLE_NAME_1: 1,
    EXAMPLE_NAME_2: 2,
    EXAMPLE_NAME_3: 3,
}

Can this change generate any problems with my project in future? Or maybe, is it even better solution?


Solution

  • Firstly, this change will not generate any problems with your project in future. This is the way you can go forward with.

    For a better readability. store these variables in a separate file, say config.py and whenever you need to change anything, you can change it there and will reflect everywhere.

    P.S: Don`t forget to import these variables from that file