I have a app name publisher. I run the code in publisher/views.py. It runs OK for inserting entries and newspapers = Newspaper.objects.values('newspaper_link')
我有一个应用名称发布者。我在publisher / views.py中运行代码。插入条目和报纸运行正常= Newspaper.objects.values('newspaper_link')
Tables are OK when I view it in the SQLite DB Browser. But when I run
当我在SQLite数据库浏览器中查看表时,表是可以的。但是当我跑步的时候
newspapers = Newspaper.objects.values('country_id')
or
要么
newspapers = Newspaper.objects.values()
It raise
它提高了
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
django.core.exceptions.AppRegistryNotReady:尚未加载模型。
Here is my publisher/models.py:
这是我的发布者/ models.py:
from django.db import models
class Country(models.Model):
country_link = models.URLField()
country_name = models.TextField(primary_key=True)
class Newspaper(models.Model):
newspaper_link = models.URLField(primary_key=True)
newspaper_name = models.TextField()
country = models.ForeignKey(Country)
Here is my traceback information:
这是我的追溯信息:
Traceback (most recent call last):
File "D:/teamproject/DjangoProjectOne/DjangoProjectOne/mysite/publisher/views.py", line 99, in <module>
preOrder('a')
File "D:/teamproject/DjangoProjectOne/DjangoProjectOne/mysite/publisher/views.py", line 78, in preOrder
newspapers = Newspaper.objects.values()
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\db\models\manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\db\models\query.py", line 606, in values
return self._clone(klass=ValuesQuerySet, setup=True, _fields=fields)
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\db\models\query.py", line 960, in _clone
c._setup_query()
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\db\models\query.py", line 1136, in _setup_query
self.query.add_fields(self.field_names, True)
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\db\models\sql\query.py", line 1692, in add_fields
name.split(LOOKUP_SEP), opts, alias, allow_many=allow_m2m)
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\db\models\sql\query.py", line 1466, in setup_joins
names, opts, allow_many, fail_on_missing=True)
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\db\models\sql\query.py", line 1367, in names_to_path
if field.is_relation and not field.related_model:
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\utils\functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\db\models\fields\related.py", line 110, in related_model
apps.check_models_ready()
File "C:\Users\Joy Zhang\Anaconda3\lib\site-packages\django\apps\registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Here is my INSTALLED_APPS in settings:
这是我在设置中的INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rss',
'tweet',
'publisher'
)
2 个解决方案
#1
1
From the traceback, you appear to be calling your preOrder
function at module level. You don't say what that function is doing, but that is definitely the cause of your error. You should only call model methods from within views, or from functions called from a view.
从回溯中,您似乎在模块级别调用preOrder函数。你没有说那个函数在做什么,但这肯定是你错误的原因。您应该只从视图内或从视图调用的函数中调用模型方法。
#2
0
I get the exception when I run standalone views.py on pyCharm so the models are not registered. And I add the following code and then it works
当我在pyCharm上运行独立的views.py时,我得到了异常,所以模型没有注册。我添加以下代码然后它的工作原理
import os, sys
proj_path = "D:/teamproject/mysite"
# This is so Django knows where to find stuff.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
sys.path.append(proj_path)
# This is so my local_settings.py gets loaded.
os.chdir(proj_path)
# This is so models get loaded.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
#1
1
From the traceback, you appear to be calling your preOrder
function at module level. You don't say what that function is doing, but that is definitely the cause of your error. You should only call model methods from within views, or from functions called from a view.
从回溯中,您似乎在模块级别调用preOrder函数。你没有说那个函数在做什么,但这肯定是你错误的原因。您应该只从视图内或从视图调用的函数中调用模型方法。
#2
0
I get the exception when I run standalone views.py on pyCharm so the models are not registered. And I add the following code and then it works
当我在pyCharm上运行独立的views.py时,我得到了异常,所以模型没有注册。我添加以下代码然后它的工作原理
import os, sys
proj_path = "D:/teamproject/mysite"
# This is so Django knows where to find stuff.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
sys.path.append(proj_path)
# This is so my local_settings.py gets loaded.
os.chdir(proj_path)
# This is so models get loaded.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()