AppRegistryNotReady("Models aren't loaded yet.")

So I’m trying to integrate a model with mongoDB usng djongo driver, and when I tried to insert a record from within models.py it is giving me this error.

AppRegistryNotReady("Models aren't loaded yet.")

here are my code files.
models.py

from djongo import models

class Iname(models.Model):
    item_name = models.TextField()
    
    class Meta:
        abstract = True
        
class Iquantity(models.Model):
    item_quantity = models.FloatField()
    
    class Meta:
        abstract = True
        
class Iprice(models.Model):
    item_price = models.FloatField()
    
    class Meta:
        abstract = True
    
class Order(models.Model):
    email = models.EmailField(primary_key = True, name = "email") 
    name = models.CharField(max_length=30, help_text="Enter Customer Name", name = "name")
    address = models.TextField(help_text="Enter customer's Address", name = "address")
    
    item_names = models.ArrayField(model_container = Iname)
    item_quantities = models.ArrayField(model_container = Iquantity)
    item_prices = models.ArrayField(model_container = Iprice)
    
    objects = models.DjongoManager()
    
o = Order()
o.email = "jayesh@gmail.com"
o.name = "dhsdb"
o.address = "agdkhdvf"
o.item_names = ['chocolate', 'pencil']
o.item_quantities = [5, 10]
o.item_prices = [10, 3]
o.save()

serializers.py

from .models import Order, Iname, Iprice, Iquantity
from rest_framework import serializers

class ItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = Iname
        
class QuantitySerializer(serializers.ModelSerializer):
    class Meta:
        model = Iquantity

class PriceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Iprice

    
class OrderSerializer(serializers.ModelSerializer):
    item_names = ItemSerializer()
    item_quantities = QuantitySerializer()
    item_prices = PriceSerializer()
    
    class Meta:
        model = Order
        fields = "__all__"
   

views.py

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serializers import OrderSerializer
from .models import Order

@api_view(['GET',])
def get_orders(request):
    orders = Order.objects.all()
    serializer = OrderSerializer(instance = orders, many = True)
    print(serializer.data)
    return Response(data = serializer.data)

I’m a newbie and trying to get my hands on django. Please have a look and explain that where and why I’m wrong/ Thank you.

And yes here is my traceback

C:\Users\ACER\Desktop\invoice\base\models.py changed, reloading.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "c:\users\acer\anaconda3\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "c:\users\acer\anaconda3\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\core\management\__init__.py", line 398, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.import_models()
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\apps\config.py", line 304, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\users\acer\anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module   
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\ACER\Desktop\invoice\base\models.py", line 64, in <module> 
    o.save()
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\base.py", line 806, in save
    self.save_base(
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\base.py", line 857, in save_base
    updated = self._save_table(
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\base.py", line 970, in _save_table
    updated = self._do_update(
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\base.py", line 1034, in _do_update
    return filtered._update(values) > 0
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\query.py", line 885, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\sql\compiler.py", line 1783, in execute_sql
    cursor = super().execute_sql(result_type)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\sql\compiler.py", line 1348, in execute_sql
    sql, params = self.as_sql()
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\sql\compiler.py", line 1749, in as_sql
    val = field.get_db_prep_save(val, connection=self.connection)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\djongo\models\fields.py", line 229, in get_db_prep_save
    processed_value = self._save_value_thru_fields('get_db_prep_save',
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\djongo\models\fields.py", line 344, in _save_value_thru_fields
    post_dict = super()._save_value_thru_fields(func_name,
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\djongo\models\fields.py", line 155, in _save_value_thru_fields
    for field in self.model_container._meta.get_fields():
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\options.py", line 849, in get_fields
    return self._get_fields(
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\options.py", line 928, in _get_fields
    all_fields = self._relation_tree
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\utils\functional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\options.py", line 822, in _relation_tree
    return self._populate_directed_relation_graph()
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\db\models\options.py", line 789, in _populate_directed_relation_graph
    all_models = self.apps.get_models(include_auto_created=True)
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\apps\registry.py", line 181, in get_models
    self.check_models_ready()
  File "C:\Users\ACER\Desktop\invoice\env\lib\site-packages\django\apps\registry.py", line 143, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.  

list of dependencies i’m using

asgiref==3.5.2
backports.zoneinfo==0.2.1  
Django==4.0.5
djangorestframework==3.13.1
djongo==1.3.6
pymongo==3.12.3
pytz==2022.1
sqlparse==0.2.4
tzdata==2022.1

Hey there.
Look’s like you are trying to create a order, but you can’t do that yet.
Django needs to load all apps before you can use the ORM.

If you did really mean to create this object, maybe you’re trying to add some test data to your database. If that’s the case maybe you should look this: How to provide initial data for models | Django documentation | Django.

If not, this code should live inside a view, or into the admin.

2 Likes