lch
lch

Reputation: 4931

Django: Error while importing a model in custom services file

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 model

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")

error

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.

I wrote a celery task in services.py file. services.py is a custom file I created in twitterdata app to put my business logic. The error started after adding the import statement in services.py file from twitterdata.models import Tweet. when I commented this import statement, everything is working and the celery task got fired. Or it could be because of from twitterdata.services import TwitterService in celery.py. Is twitterdata app not loaded by the time I used that import statement (from twitterdata.services import TwitterService) in celery.py? Can someone help me in finding the issue? BTW i added twitterdata app to INSTALLED_APPS in settings.py

enter image description here

Upvotes: 0

Views: 99

Answers (1)

James Bennett
James Bennett

Reputation: 11163

Your celery.py is importing services.py before it configures Django's settings (by setting DJANGO_SETTINGS_MODULE). And your services.py imports a Django model. That means services.py is trying to access a model before Django has fully finished configuring and loading everything, which is why you get the error.

Move the import of TwitterService inside setup_periodic_tasks() in celery.py and see if that fixes it (since that will delay the import until after settings and app registry are available).

Upvotes: 1

Related Questions