python-3.xdjangodjango-modelsvisual-studio-2022

Apps aren't loaded yet


I'm using Visual Studio 2022 as the IDE. It has 2 projects:

  1. CLI - A Python Application
  2. Common - A Blank Django Project

The classes are created in Common > app > models.py.
app is a Django app

enter image description here

CLI is the startup project, and cli.py is its startup file.
Common project is referenced in CLI.

enter image description here

When I Start the CLI app in debug mode, there is an error for the Question class.

enter image description here

Call Stack:

 Message=Apps aren't loaded yet.  
 Source=D:\Projects\Internal\LLRT\src\Common\app\models.py  
 StackTrace:   File
 "D:\Projects\Internal\LLRT\src\Common\app\models.py", line 4, in
 <module> (Current frame)
     q_id = models.IntegerField()
     ...<7 lines>...
     correct_option_num = models.IntegerField()   File "D:\Projects\Internal\LLRT\src\CLI\cli.py", line 4, in <module>
>     from app.models import Option, Question, Answered_Question django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Solution

  • I resolved this by adding the statement django.setup().
    It works now, but it just looks odd that the statement is in the import statements section.
    Please let me know if there is a better way of doing this.

    enter image description here