django - Django 应用程序尚未加载 - 无法导入模型
问题描述
我无法将我的模型导入到我的 celery.py 文件中,所以我在计划任务中使用它 - 我总是得到django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
我正在撕扯我的头发 - 一些人似乎遇到了同样的错误,但在同样的情况下没有一个人,我尝试了所有的修复,但没有任何效果。
我的主 Django 应用程序中的 celery.py 文件:
from __future__ import absolute_import, unicode_literals
import requests
import os
from celery import Celery
from WeatherData.models import LondonWeather
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Weather.settings')
from .settings import OPEN_WEATHER_API_KEY, OPEN_WEATHER_API_URL
# set the default Django settings module for the 'celery' program.
app = Celery('Weather')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
# Gets London weather every hour.
sender.add_periodic_task(10.0, get_weather_task.s(), name='london_weather_test')
sender.add_periodic_task(3600.0, get_weather_task.s(), name='london_weather')
@app.task()
def get_weather_task():
querystring = {"q": "London,UK"}
headers = {
'x-api-key': OPEN_WEATHER_API_KEY,
}
res = requests.get(OPEN_WEATHER_API_URL, headers=headers, params=querystring).json()
LondonWeather.objects.create(
longitude=res.get('coord', 0).get('lon', 0),
latitude=res.get('coord', 0).get('lat', 0),
main_weather=res.get('weather', {})[0].get('main', 'Rain'),
description=res.get('weather', {})[0].get('description', 'No data'),
temperature=res.get('main', {}).get('temp', 0),
pressure=res.get('main', {}).get('pressure', 0),
humidity=res.get('main', {}).get('humidity', 0),
min_temp=res.get('main', {}).get('temp_min', 0),
max_temp=res.get('main', {}).get('temp_max', 0),
wind_speed=res.get('wind', {}).get('speed', 0),
wind_direction=res.get('wind', {}).get('deg', 0),
clouds=res.get('clouds', {}).get('all', 0),
)
return res
由于 clery,我的主应用程序的init .py 看起来像这样:
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)
这与 Django 的导入有什么关系吗?
解决方案
推荐阅读
- sql - AutoMapper SQL 优化映射投影生成(NonNullable To Nullable)
- c# - 为什么Dictionary在这里变成了KeyValuePair?
- visual-studio-code - 如何使用 sublime 键盘快捷键等一个键绑定向选择或整行添加评论?
- c# - 列表
加入数据表 - python - 如果因变量既有正值又有负值,如何处理任何机器学习算法?
- aws-amplify - 我可以使用 AWS Amplify 创建在将新图像上传到 S3 时触发 lambda 函数的 SQS
- javascript - 使用 javascript 时无法调整设计
- c# - SQL/VisualStudio 的“插入语句”中的多线程
- node.js - 在单个查询中计算 mongoose 中的文档
- apache-kafka - Kafka excatly-once 生产者消费者