Pycharm debugging Django core. exceptions. AppRegistryNotReady: Apps aren‘t loaded yet.

JaquanC 2022-05-14 15:11:57 阅读数:901


Demand background

I need to django In the project app Apply the following tool method , Used to update the database .

Error message

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Valid code

import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'devops.settings') # devops Change to your own project name 
from blog.models import Aticle # Import your own application model here 
def update_single_content():
c_ids = Aticle.objects.values_list("article_id", flat=True)
c_ids = list(c_ids)
if __name__ == '__main__':

Here we need to pay attention to the former 4 Line code , Don't be in the wrong order

  • AppRegistryNotReady Starting from this error report , The solution should be to load the application models Settings for previously activated projects , therefore django.setup() Must be in update_single_content Before .

  • os.environ.setdefault Set the running environment of the environment , Exclude right pycharm The configuration of depends on , No use pycharm Add environment variables .