Giter Site home page Giter Site logo

Comments (13)

dmsimard avatar dmsimard commented on May 18, 2024 1

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.

apollo13 avatar apollo13 commented on May 18, 2024 1

@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.

rochacbruno avatar rochacbruno commented on May 18, 2024

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.

rochacbruno avatar rochacbruno commented on May 18, 2024

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.

dkliban avatar dkliban commented on May 18, 2024

Thank you! I was able to follow your advice here[0].

[0] https://github.com/pulp/pulp/pull/3678/files

from dynaconf.

rochacbruno avatar rochacbruno commented on May 18, 2024

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 in manage.py, wsgi.py and any other entry point of your application intead of INSTALLED_APPS use INSTALLED_APPS only in cases where you don't need the DATABASES key to be managed by dynaconf."
  • 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.

mspinelli avatar mspinelli commented on May 18, 2024

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.

rochacbruno avatar rochacbruno commented on May 18, 2024

@mspinelli the proposed solution is not implemented yet, it needs to be implemented.

Right now to use django there are 2 solutions

    1. Load dynaconf in INSTALLED_APPS knowing that DATABASE should be kept on django's settings.
    1. Load dynaconf in all the entry points as manage.py, wsgi.py and others.

from dynaconf.

rochacbruno avatar rochacbruno commented on May 18, 2024

@dmsimard @apollo13 can we expect a Pull Request?

from dynaconf.

jasisz avatar jasisz commented on May 18, 2024

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.

rochacbruno avatar rochacbruno commented on May 18, 2024

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.

rochacbruno avatar rochacbruno commented on May 18, 2024

IN progress on #126

from dynaconf.

rochacbruno avatar rochacbruno commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.