Comments (13)
Hi o/
By the way, I think @apollo13 had a good idea by getting dynaconf to load settings from within settings instead of patching entrypoints for this issue.
You can see the commit here: recordsansible/ara-server@fdcc003
from dynaconf.
@rochacbruno Against what? My changes just use dynaconf directly and drop the Django integration. There is not really anything to patch with that regard in dynaconf itself…
from dynaconf.
Looks like Django is accessing DATABASES
before the load of INSTALLED_APPS
so I can think in a way to fix it.
Load dynaconf explicitly in manage.py
and in wsgi.py
after this line.
https://github.com/rochacbruno/dynaconf/blob/master/example/django_example/manage.py#L6
or calling it on settings.py
bottom.
from dynaconf.
I want to add an implementation targetting Python 3.7 using this https://www.python.org/dev/peps/pep-0562/
and also I am going to try other solutions.
If we figureout that the proper way is calling dynaconf on manage.py
and wsgi.py
then I'll add this to the docs.
from dynaconf.
Thank you! I was able to follow your advice here[0].
[0] https://github.com/pulp/pulp/pull/3678/files
from dynaconf.
I did an extensive dive in to Django source code today,
The case is that Django is creating the database connection before it loads the INSTALLED_APPS
where dynaconf is supposed to be loaded.
Django is doing that because there is a console hint saying You have N pending migrations to be applied...
and to calculate that Django fires the migration check before the INSTALLED_APPS are loaded.
This is part of https://docs.djangoproject.com/en/1.11/ref/checks/#database
The only setting that Django is looking before INSTALLED_APPS
is DATABASES
One of the possible solution is introducing a new environment variable for Django so users will always need to export the following 2 variables to enable Dynaconf:
export DJANGO_SETTINGS_MODULE=dynaconf.contrib.django_settings
export SETTINGS_MODULE_FOR_DYNACONF=myprogram.app.settings
Then internally the dynaconf.contrib.django_settings
resolves the load of myprogram.app.settings
the solution works seemlesly without the need to even change the INSTALLED APPS, the good part is that to enable dynaconf the user only needs to change envvars and no need to touch any code.
I want to hear more opinions about this idea ^.
For now I will:
- Keep this issue opened until we find a better solution (Still need to try the Django AppConfig class)
- Send an email do Django dev list asking for help/suggestions
- Include in the Dynaconf docs a note saying that:
- "If you need the
DATABASES
to be rewritable please load dynaconf inmanage.py
,wsgi.py
and any other entry point of your application intead ofINSTALLED_APPS
use INSTALLED_APPS only in cases where you don't need the DATABASES key to be managed by dynaconf."
- "If you need the
- For Python 3.7 I'll include an implementation using the new
module.__getattr__
method
IN all the cases, the backwards compatibility should never break! so the way you are loading dynaconf today should still work and the way using INSTALLED_APPS should also work. My plan is to introduce other ways to load it.
from dynaconf.
Any progress on this problem?
I want to hear more opinions about this idea ^.
I haven't tried the dual environment variable export yet. Are you saying this works out of the box already this way, or that it would need more code changes in dynaconf to make this work? E.g. dynaconf.contrib.django_settings
doesn't seem to exist.
That said I am on Python 3.7. What does the module.__getattr__
method look like to solve this problem?
from dynaconf.
@mspinelli the proposed solution is not implemented yet, it needs to be implemented.
Right now to use django there are 2 solutions
-
- Load dynaconf in
INSTALLED_APPS
knowing that DATABASE should be kept on django's settings.
- Load dynaconf in
-
- Load dynaconf in all the entry points as
manage.py
,wsgi.py
and others.
- Load dynaconf in all the entry points as
from dynaconf.
@dmsimard @apollo13 can we expect a Pull Request?
from dynaconf.
I've found two more Django corner-cases.
One is ALLOWED_HOSTS
- even if you change it with dynaconf, it still will have default value somewhere internally and requests will fail.
The other case is when you use settings.SOME_CONFIG
in your templates - in such scenario dynaconf fails with 'Settings' object has no attribute '__name__'
- this is because it goes through the django.templates.base._resolve_lookup
which inspects with getcallargs on LazySettings
.
from dynaconf.
The template problem must e resolved with the recommendation to use ['key
]` syntax.
The other problems there is a way to fix using getattr but only for python 3.7+
from dynaconf.
IN progress on #126
from dynaconf.
2.0.0 released DATABASE stuff is now fixed.
read more in https://dynaconf.readthedocs.io/en/latest/guides/django.html
example in https://github.com/rochacbruno/dynaconf/tree/master/example/django_example
@jasisz can you please test the render template? or add a reproducer on the https://github.com/rochacbruno/dynaconf/tree/master/example/django_example
Thanks!
from dynaconf.
Related Issues (20)
- [RFC] Pydantic Schema Validation HOT 3
- Centralized config package | External hooks for `platformdirs`, __package__, package_dir, etc. HOT 1
- [bug] Environment Variable Overrides Not Working with Nested .toml Values HOT 1
- [bug] Dynaconf.load_file() no error on missing file(s)
- Documentation used to be clear on purpose of global, and the default environment HOT 2
- Multiple cast validators get discarded
- Broken link to source code in docs HOT 1
- Standard docstrings style for the codebase HOT 3
- Validator default string parsed to number HOT 3
- [bug] Validation on Dynaconf instantiation not working HOT 1
- Validation doc section "On instantiation" improvement HOT 2
- [CI] New release process HOT 2
- [RFC] Add `as_dict` alias to `to_dict` for `DynaBox` for consistency between `LazySettings` and `DynaBox` objs HOT 1
- [CI] Update codecov configuration file
- [RFC] Add FORCE_SETTINGS_FILES to LazySettings.configure() for pytest
- [RFC] Use profile link of a contributor in the CHANGELOG
- [RFC] Provide workaround for auto-complete w/ Schema HOT 2
- [bug] Loading config values failing in Python 3.11 while it works in Python 3.10 HOT 1
- [bug] @reverse_lazy doesn't get evaluated without settings.using_env HOT 4
- [bug] fresh_vars items are assignable HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dynaconf.