Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to store constants in Django

I have a Django application and use GitHub for CI. Now I run into the problem that I have a merge conflict with constants import every time I merge. Another developers are importing constants vary often, so do I. Directories tree looks like that:

main_app > 
...main_app 
...api 
...aws_lambda 
...billing 
...utils 
...and many other directories

Each sub-application has its own file constants.py. Constants import looks like that:

from utils.constants import const1, const2, const3, const4 
from billing.constants import const5, const6

How I need to rewrite those imports to decrease merge conflicts in future? Is there a better way than the one below?

import utils.constants as utils_const
import billing.constants as billing_const
...
var = utils_const.const1

What is the best practice to store constants in Django app?

like image 604
v18o Avatar asked Jan 11 '18 11:01

v18o


Video Answer


1 Answers

For constants that will only be used by one module, just define them in that module. For constants that are used by the whole project, the convention is to add them to your settings file. For constants used throughout a single app, I think your approach of having a constants.py per app is fine.

like image 196
sbywater Avatar answered Sep 19 '22 02:09

sbywater