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

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

pycharmdebuggingdjangocore.core

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 
django.setup()
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)
print(c_ids)
if __name__ == '__main__':
update_single_content()

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 .

版权声明:本文为[JaquanC]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205141509564397.html