Django:在自定义服务文件中导入模型时出错

时间:2018-04-08 17:00:54

标签: python django celery

celery.py

import os
from celery import Celery
from django.conf import settings
from twitterdata.services import TwitterService


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TwitterApiProxy.settings')

app = Celery('TwitterApiProxy')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    print("calling load_tweets task")
    sender.add_periodic_task(10.0, TwitterService.load_tweets.s(), name='add every 10')

Tweet.py模型

from django.db import models


class Tweet(models.Model):
    id = models.BigIntegerField
    title = models.CharField
    image = models.CharField
    url = models.CharField

    @classmethod
    def create(cls, tweet_id, dictionary):
        print(dictionary)
        tweet = cls(id=tweet_id, title=dictionary['title'], image=dictionary['image'], url=dictionary['url'])
        return tweet

services.py

import twitter
import requests
from celery import shared_task
from twitterdata.models import Tweet


class TwitterService:
    @staticmethod
    @shared_task
    def load_tweets():
        print("Loading Tweets")

错误

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 317, in execute
    settings.INSTALLED_APPS
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 936, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/lcherukuri/Documents/twitter-api-proxy/TwitterApiProxy/__init__.py", line 2, in <module>
    from .celery import app as celery_app
  File "/Users/lcherukuri/Documents/twitter-api-proxy/TwitterApiProxy/celery.py", line 4, in <module>
    from twitterdata.services import TwitterService
  File "/Users/lcherukuri/Documents/twitter-api-proxy/twitterdata/services.py", line 4, in <module>
    from twitterdata.models import Tweet
  File "/Users/lcherukuri/Documents/twitter-api-proxy/twitterdata/models.py", line 4, in <module>
    class Tweet(models.Model):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/registry.py", line 127, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

我在services.py文件中写了一个芹菜任务。 services.py是我在twitterdata app中创建的自定义文件,用于放置我的业务逻辑。在services.py文件from twitterdata.models import Tweet中添加import语句后,错误已开始。当我评论这个import语句时,一切正常,celery任务被解雇了。或者可能是因为from twitterdata.services import TwitterService中的celery.py。我在twitterdata中使用该导入语句(from twitterdata.services import TwitterService)时是否未加载celery.py应用?有人可以帮助我找到问题吗? BTW我在twitterdata

中将INSTALLED_APPS个应用添加到settings.py

enter image description here

1 个答案:

答案 0 :(得分:1)

您的celery.py在配置Django设置之前导入services.py(通过设置DJANGO_SETTINGS_MODULE)。而你的services.py导入一个Django模型。这意味着services.py在Django完全配置并加载所有内容之前尝试访问模型,这就是您收到错误的原因。

TwitterService内的setup_periodic_tasks()导入celery.py内,并查看是否修复了它(因为这会延迟导入,直到设置和app注册表可用后)。