1👍
✅
You can load it from your apps.py
file.
In your django app (Lets’ call your app myapp), you may have a file called apps.py
in the same level as your models.py
, views.py
, etc
You override the ready()
method in there.
Example in myapp/apps.py
from django.apps import AppConfig
loaded_model = None
class MyAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myapp'
def ready(self):
global loaded_model
loaded_model = <load your model here>
Update in your app in INSTALLED_APPS
settings in settings.py
like this
'myapp.apps.MyAppConfig'
You can then use this in your views.py
like this
from .apps import loaded_model
def my_view(request):
result = loaded_model.<your_model_method>
Source:stackexchange.com