0

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
BDL
  • 21,052
  • 22
  • 49
  • 55
  • Yes. The documentation specifically warns against importing models from the `__init__.py`. Why are you starting your mqtt client there? – Daniel Roseman Jan 20 '17 at 11:35
  • I need to listen the mqtt broker from ryt from the time my server starts without any blockage for the loop @DanielRoseman – sekar navin Jan 20 '17 at 12:58
  • Move the import inside the function so it only loads when executed which is probably after django has initialized. – Anonymous Jan 20 '17 at 23:49

0 Answers0