Can U tell me how to access models before all the apps gets loaded
This is my mqtt.py which was initialized in init.py and accessing
models
import paho.mqtt.client as mqtt
from Transport.models import BusPosition
# Broker CONNACK response
def on_connect(client,userdata,flags,rc):
print ("Connected with result code "+str(rc))
# Subcribing to topic and recoonect for
client.subscribe("trackrkct")
#Receive message
def on_message(client,userdata,msg):
print(msg.topic+" "+str(msg.payload))
BusPosition.create(1, "Mon, 23 May 2016 08:30:15 GMT", 11.0998, 77.9098)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.hivemq.com",1883,60)
This is my init.py where mqtt.py was initialized
from kctsmarttransport import mqtt
mqtt.client.loop_start()
and this is my models.py
from __future__ import unicode_literals
from django.db import models
class BusPosition(models.Model):
bus = models.ForeignKey(Bus)
time = models.DateTimeField
lat = models.FloatField(max_length=20)
lng = models.FloatField(max_length=20)
#classmethod
def create(cls,bus,time,lat,lng):
busPosition = cls(bus=bus,time=time,lat=lat,lng=lng)
busPosition.save()
return busPosition
and I got these errors as apps aren't loaded yet
"C:\Program Files (x86)\JetBrains\PyCharm 2016.2.3\bin\runnerw.exe" C:\Users\Navin\DjangoProject\kct_transport_env\Scripts\python.exe C:/Users/Navin/DjangoProject/kctsmarttransport/manage.py runserver 5000
Traceback (most recent call last):
File "C:/Users/Navin/DjangoProject/kctsmarttransport/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Navin\DjangoProject\kct_transport_env\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\Navin\DjangoProject\kct_transport_env\lib\site-packages\django\core\management\__init__.py", line 316, in execute
settings.INSTALLED_APPS
File "C:\Users\Navin\DjangoProject\kct_transport_env\lib\site-packages\django\conf\__init__.py", line 53, in __getattr__
self._setup(name)
File "C:\Users\Navin\DjangoProject\kct_transport_env\lib\site-packages\django\conf\__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\Navin\DjangoProject\kct_transport_env\lib\site-packages\django\conf\__init__.py", line 97, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\Navin\DjangoProject\kctsmarttransport\kctsmarttransport\__init__.py", line 1, in <module>
from kctsmarttransport import mqtt
File "C:\Users\Navin\DjangoProject\kctsmarttransport\kctsmarttransport\mqtt.py", line 2, in <module>
from Transport.models import BusPosition
File "C:\Users\Navin\DjangoProject\kctsmarttransport\Transport\models.py", line 6, in <module>
class Bus(models.Model):
File "C:\Users\Navin\DjangoProject\kct_transport_env\lib\site-packages\django\db\models\base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\Navin\DjangoProject\kct_transport_env\lib\site-packages\django\apps\registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\Navin\DjangoProject\kct_transport_env\lib\site-packages\django\apps\registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Process finished with exit code 1
Related
I will import my models in celery.py. But when I import and run the runserver command, I get the following error:
File "/directory/manage.py", line 22, in <module>
main()
File "/directory/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/directory/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/directory/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 363, in execute
settings.INSTALLED_APPS
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/directory/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
......
File "/directory/__init__.py", line 1, in <module>
from .celery import app as celery_app
File "/directory/celery.py", line 9, in <module>
from apps.models import Model1
File "/directory/apps/models.py", line 2, in <module>
from django.contrib.auth.models import User, AbstractUser
File "/directory/venv/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/directory/venv/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/directory/venv/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "/directory/venv/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "/directory/venv/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
My import code line in celery.py:
from app.models import model1, model2
You can try adding this line to your settings.py file:
import django
django.setup()
See here for solution.
if it doesn't work, this might be what you need:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
and these lines:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
If the first solution doesn't work here the alternative solution.
Finally, there is such a solution:
import django
# some variable declarations
world_mapping = {
'osm_id': 'osm_id',
}
if __name__ == '__main__':
django.setup()
# import AFTER setup
from app.models import WorldBorder
# from now I can access WorldBorder!!
I solved it by copying all tasks from celery.py to app tasks.py.
It also fixes itself when I delete celery related lines from __init__.py file.
I wanted to add the first model to an already working app, and now I can't start it because it always gives AppRegistryNotReady. This does only happen, if my model MailLog is child of models.Model.
from __future__ import unicode_literals
from django.db import models
#class MailLog(models.Model): # like this, it crashes
class MailLog(): # like this, it works
# Field definitions
recipient = models.EmailField()
created = models.DateTimeField(auto_now_add=True)
template = models.CharField(max_length=500)
error = models.TextField(null=True)
def __str__(self):
return self.recipient+" "+self.template+"("+str(self.created)+")"
The error occurs no matter what's inside the class, even if there is only a pass. However, I can import my models in the admin.py, and it crashes when I import from a file I called core.py. That file looks like this:
import boto3
from botocore.exceptions import ClientError
from django.template import loader
from django.conf import settings
from .templates import *
from .models import MailLog
The traceback looks like this
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 316, in execute
settings.INSTALLED_APPS
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/niels/anaconda3/envs/app/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/niels/Dokumente/jobapp/deploy/backend/settings.py", line 255, in <module>
django.setup()
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/home/niels/anaconda3/envs/app/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/niels/Dokumente/jobapp/deploy/ses_mail/__init__.py", line 1, in <module>
from .core import send
File "/home/niels/Dokumente/jobapp/deploy/ses_mail/core.py", line 7, in <module>
from .admin import MailLog
File "/home/niels/Dokumente/jobapp/deploy/ses_mail/admin.py", line 2, in <module>
from .models import *
File "/home/niels/Dokumente/jobapp/deploy/ses_mail/models.py", line 6, in <module>
class MailLog(models.Model):
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/db/models/base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
You should not be importing Django models in your app's __init__ (or causing them to be imported by importing from core. If you use an empty __init__.py it should stop the error.
I'm upgrading from django 1.6.5 to django 1.9, and in the process upgrading several middleware classes. Some of those middleware classes use models during the process_request or process_response phases. However, I'm getting a AppRegistryNotReady: Apps aren't loaded yet. error attempting to use them.
Is there a way to import models during middleware?
Should I move my import statements into the process_request / process_response methods?
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/api/web_transaction.py", line 1329, in _nr_wsgi_application_wrapper_
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/api/web_transaction.py", line 1329, in _nr_wsgi_application_wrapper_
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 158, in __call__
self.load_middleware()
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.50.0.39/newrelic/common/object_wrapper.py", line 302, in _wrapper
result = wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 51, in load_middleware
mw_class = import_string(middleware_path)
File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 20, in import_string
module = import_module(module_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/web/MyJobs/MyJobs/apache/../middleware.py", line 9, in <module>
from django.contrib.sites.models import Site
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py", line 83, in <module>
class Site(models.Model):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
AppRegistryNotReady: Apps aren't loaded yet.
You need to use the new API to get a WSGI handler:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
This will call django.setup() for you, which will populate the app registry.
I have model with attribute like this:
new = models.BooleanField(default=False, verbose_name=_("Is new"),
help_text=_("By default product is marked as new by {0} days since creation.")
.format(settings.PRODUCT_IS_NEW_EXPIRATION_DAYS))
This unfortunately works for runserver command but when I want to run test it failes and I receive following error:
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.
This error is clear. I'm trying to use translations before apps are ready.
It is possible to have a translation that requires string formatting at import time?
Edit:
Traceback:
File "./manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "django/core/management/__init__.py", line 328, in execute
django.setup()
File "django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "django/apps/config.py", line 86, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "haystack/__init__.py", line 57, in <module>
signal_processor_class = loading.import_class(signal_processor_path)
File "haystack/utils/loading.py", line 32, in import_class
module_itself = importlib.import_module(module_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "utils/signals.py", line 12, in <module>
from products.models import Products
File "products/models/__init__.py", line 2, in <module>
from .carousel import ProductCarousel, ProductCarouselImage
File "products/models/carousel.py", line 10, in <module>
from products.models.product import Products
File "products/models/product.py", line 210, in <module>
class Products(models.Model):
File "products/models/product.py", line 241, in Products
help_text=(ugettext("By default product is marked as new by {0} days since creation.")).format(
File "django/utils/translation/__init__.py", line 84, in ugettext
return _trans.ugettext(message)
File "django/utils/translation/trans_real.py", line 330, in ugettext
return do_translate(message, 'ugettext')
File "django/utils/translation/trans_real.py", line 307, in do_translate
_default = _default or translation(settings.LANGUAGE_CODE)
File "django/utils/translation/trans_real.py", line 209, in translation
_translations[language] = DjangoTranslation(language)
File "django/utils/translation/trans_real.py", line 118, in __init__
self._add_installed_apps_translations()
File "django/utils/translation/trans_real.py", line 159, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.
I tried to comment on the post but I can't since I don't have enough reputation. Have you tried formatting the help_text first and then translating it? Something like this:
help_text = _(
"By default product is marked as new by {} days since creation.".format(
settings.PRODUCT_IS_NEW_EXPIRATION_DAYS
)
)
new = models.BooleanField(default=False, verbose_name=_("Is new"),
help_text=help_text)
NOTE: .format is inside the ugettext
I have upgraded django version from 1.8 into 1.9 and django rest framework to 3.3.3. I am getting this exception:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I have tried as follows but exception is still there.
#__init__.py
default_app_config = 'panel.apps.PanelConfig'
And also
#apps.py
from django.apps import AppConfig
class PanelConfig(AppConfig):
name = 'panel'
def ready(self):
from panel import receivers
for all apps and added these to installed apps
'api.apps.ApiConfig',
'billing.apps.ApiConfig',
'incoming.apps.IncomingConfig',
'outgoing.apps.OutgoingConfig',
'panel.apps.PanelConfig',
This is my full traceback:
Unhandled exception in thread started by <function wrapper at 0x7f3eec09c7d0>
Traceback (most recent call last):
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/admin_tools/dashboard/__init__.py", line 1, in <module>
from admin_tools.dashboard.dashboards import *
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/admin_tools/dashboard/dashboards.py", line 13, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 161, in <module>
class ContentType(models.Model):
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Exception is still there ? What is the problem ? I am not getting ?
The traceback shows you that the problem is occuring in admin_tools.
from admin_tools.dashboard.dashboards import *
File "/home/sparrow/virtualenvs/bishnu/local/lib/python2.7/site-packages/admin_tools/dashboard/dashboards.py", line 13, in <module>
from django.contrib.contenttypes.models import ContentType
It looks like it has been fixed, so try upgrading to the latest release, currently 0.7.2.