-
-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. #466
Comments
Hey @vuhi , Yeah, that's a relevant problem. I think that I didn't hit it in the example cause I didn't use any models. I recall that django models can't be imported before django intialization is finished. This definitely needs to have a solution.
No, I'm not familiar with it. I'll take a look for sure. Also I'll take a look on writing a custom app that could twist in django and dependency injector initialization properly. Huge thanks for bringing this up! |
Hi, @rmk135 I finally make it works. I am pretty new to python & Django. I am not sure how Django handle import models & stuff. Here is my work around: I create an django app call
ioc/di_containers.py
I register the app at the end of settings.py
experiment/apps.py
experiment/views.py
PS:I don't understand how the wire process work. I do have a question on that. What if I override a For example:
Thank you, |
Thanks, @vuhi. I got here because I was having the same issue. Your fix worked for me. Is there anybody working on updating the docs? |
In my latest experience there is no need to put all the models to an core app etc. The problematic part from the Django example seems to be this portion: # githubnavigator/__init__.py
from .containers import Container # <--- here is the problematic part if containers.py imports/references models
from . import settings
container = Container()
container.config.from_dict(settings.__dict__) Because the # web/apps.py
from django.apps import AppConfig
from githubnavigator import container
class WebConfig(AppConfig):
name = "web"
def ready(self):
from githubnavigator import container # <-- import the container here so further imports inside container.py like models can succeed
container.wire(modules=[".views"]) # githubnavigator/containers.py
from dependency_injector import containers, providers
from github import Github
from . import services
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
github_client = providers.Factory(
Github,
login_or_token=config.GITHUB_TOKEN,
timeout=config.GITHUB_REQUEST_TIMEOUT,
)
search_service = providers.Factory(
services.SearchService,
github_client=github_client,
)
container = Container() # <-- create the container here
container.config.from_dict(settings.__dict__) Disclaimer: I am still learning Django and I am still bootstrapping my project so I can't tell something about long term issues with this approach but so far this was what made my project work again. @rmk135 Can you confirm that and maybe change the Django example? |
Hi,
Thank you so much for a wonderful project. I am using dependency injection in a Django side project of mine.
I notice a big problem with the way the container initialize. As you state in the example section for Django, we initiate the container in
__init__.py
at project level.https://python-dependency-injector.ets-labs.org/examples/django.html
However, if one of your provider (Ex: UserService needs User model) requires model to do some works, django will throw django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I am not so sure how to solve it? I am using Singleton in my container (required). Not sure if it that the reason?
Update: I solve the problem with local import. But still I have to import locally multiple places in different function. The main root cause still because of initialization of the container in
__init__.py
at project level. Let say we move it to a custom app, which register at the end of INSTALLED_APP list, we still have a problem how to wire them to the other app.I don't know if you familiar with ReactiveX. I think this package can help solve the problem of wiring https://github.com/ReactiveX/RxPY. We could have an subscriber at each AppConfig in
def ready: ...
. Then when we initiate the container, it will trigger a signal to tell them, ok it is safe for you guys to wire now ?The text was updated successfully, but these errors were encountered: