Improve Dotenv usage to avoid basic issues
Feature Request
Is your feature request related to a problem or unsupported use case?
I tried to implement the Dotenv feature in my settings and i encountered some issues when setting it from a setup method. I did this because i have settings file for environments and i though it would be nice to have a dotenv file for each environment (like .env.dev, .env.production, etc..)
Setting from 'post_setup'
Trying to set the DOTENV in post_setup don't work, which i understood lately seeing the Dotenv mechanism (Configuration.load_dotenv) is done from Configuration.pre_setup. Result is just the Dotenv path in post_setup is just ignored (i didn't tried in setup() but i think it will be the same);
Mistake sample:
class Development(Configuration):
@classmethod
def post_setup(cls):
super().post_setup()
cls.DOTENV = "/foo/bar/.env"
Setting from 'pre_setup'
Trying to set the DOTENV in pre_setup can't work, since it would need to be before a super().pre_setup() to be effective. But trying to set it before is just impossible it leads to this error:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
It was hard to debug because i did not considered a problem with Dotenv setting and the setup methods. I just found it in going back to a previous version where i didn't set DOTENV in setup methods. The error traceback was not helpful since it did not involved any point to django-configurations code or my settings. Seems the usage of cls before the setup cause problems.
Mistake sample:
class Development(Configuration):
@classmethod
def pre_setup(cls):
cls.DOTENV = "/foo/bar/.env"
super().pre_setup()
Describe the solution you'd like
Update the documentation part which introduces DOTENV setting and put a warning to list these usage mistakes.
Describe alternatives you've considered
From what i saw i don't think the setup mechanism and settings loader are able to properly detect these usage mistakes.
Do you want to work on it through a Pull Request?
I could try to apply a documentation change in a pull request if needed. I didn't do it yet since i would want your advice on this issue before, since i'm not sure i missed something.
I'm sorry but after more digging, i've finally found that the issue with pre_setup() was wrong, in fact it was only that the Dotenv filepath did not exists. As i assumed that the Dotenv file loading was safe, i did not created a Dotenv filepath yet before testing.
I don't know why this leaded to the exception django.core.exceptions.AppRegistryNotReady, maybe because i'm composing my setting class from other classes, however i made the DOTENV conditionnal to the Dotenv filepath existence and it correctly works.
Just maybe insist in documentation about the defined Dotenv filepath in DOTENV must exists ?