我正在尝试从“用户”应用程序导入表,但是它一直失败。在此之前,我已将应用程序中的表导入到另一个应用程序中的其他文件中,没有错误。
这是我尝试从应用程序内部运行脚本时的堆栈跟踪:
Traceback (most recent call last):
File "trending_tweets.py", line 9, in <module>
from users.models import Country
ModuleNotFoundError: No module named 'users'
这是trending_tweets.py
文件:
import yweather
import tweepy
from decouple import config
# from django.apps import apps
from users.models import Country
# countries = apps.get_model('users', 'Country')
class TrendingTweets:
"""
Class to generate trending tweets within bloverse
countries
"""
def __init__(self):
"""
configuration settings to connect twitter API
at the point of initialization.
"""
self.api_key = config('TWITTER_API_KEY')
self.twitter_secret_key = config('TWITTER_SECRET_KEY')
self.access_token = config('ACCESS_TOKEN')
self.access_token_secret = config('ACCESS_TOKEN_SECRET')
def twitter_api(self):
"""
authentication method to configure twitter settings.
"""
auth = tweepy.OAuthHandler(self.api_key, self.twitter_secret_key)
auth.set_access_token(self.access_token, self.access_token_secret)
api = tweepy.API(auth)
return api # auth request object
def generate_woeid(self):
"""
method to generate WOEID of each country
on our platform
"""
client = yweather.Client()
woeid_box = []
countries = Country.objects.all()
for country in countries:
woeid = client.fetch_woeid(country.name)
woeid_box.append(woeid)
return woeid_box
if __name__ == '__main__':
x = TrendingTweets()
r = x.generate_woeid()
print(r)
阅读有关循环进口的信息,但仍然找不到解决此问题的方法。我在做什么错了?
这是我的文件夹结构:
此外,使用:
from django.apps import apps
countries = apps.get_model('users', 'Country')
返回此错误:
Traceback (most recent call last):
File "trending_tweets.py", line 11, in <module>
countries = apps.get_model('users', 'Country')
File "/home/myPC/Documents/CODE/venv/lib/python3.6/site-packages/django/apps/registry.py", line 190, in get_model
self.check_models_ready()
File "/myPC/myPc/Documents/CODE/venv/lib/python3.6/site-packages/django/apps/registry.py", line 132, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
我的settings.py INSTALLED_APPS
列表:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# local apps
'users',
'api',
'posts',
'generator',
# third-party
'rest_framework',
'rest_framework.authtoken',
'rest_framework_swagger',
'corsheaders',
]
答案 0 :(得分:2)
使用apps
模块导入。
from django.apps import apps
mymodel = apps.get_model('users', 'Country')
还要确保您在INSTALLED_APPS
的{{1}}中正确订购了这些应用。以错误的顺序加载它们可能导致模块不加载。
您可以了解有关here,
的更多信息