6
votes

i have the following task:

from __future__ import absolute_import

from myproject.celery import app

from myapp.models import Entity


@app.task
def add(entity_id):
    entity = Entity.objects.get(pk=entity_id)
    return entity.name

I get the following error:

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

If I take out the entity import every thing is fine and no error occurs. When add back :

from myapp.models import Entity

the error returns.

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
from django.core.mail import EmailMultiAlternatives
from django.template import Context, loader
from django.utils.html import strip_tags

class Entity(models.Model):
    area = models.ForeignKey(Area)
    name = models.CharField(max_length=255)
    type = models.CharField(max_length=255)
    status = models.IntegerField(choices=STATUS_TYPES, default=0)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)


    def __unicode__(self):
        return self.name

How do I import a django model into a celery task?

1
Can you post Entity model? You're probable trying to import settings from that file, and try to access to database by it. Or some dependent does. Hard to tell from the info you gave. - 0xmtn
@mtndesign I added entity model. If I was importing a setting or user what would I need to do? - Atma
Well I didn't mean the model itself, i meant the model file, to see the imported libs. Anyways, if you were importing settings module, you need to make sure that it is available and accessible from the path the Entity model is in. The error claims that you did not configured your app properly, in a way that django doesn't know where the essential files are. To configure you can use this: from django.conf import settings then settings.configure(). - 0xmtn
@mtndesign I've added the imports. I don't import settings, but when I configure like you mentioned with settings, I get the error django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. I then add: import django django.setup() and I get the error: RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. - Atma
Are you sure you created the app correctly? It seems your settings file is not filled up to meet the needs of your app. And from the previous error I can tell that you didn't create the app correctly, too. Add your app name to INSTALLED_APPS in settings. And also check this tutorial to figure out misconfigurations in your app: docs.djangoproject.com/en/1.9/intro/tutorial01 - 0xmtn

1 Answers

6
votes

My celery file needed to have:

from __future__ import absolute_import

import os

from celery import Celery


# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

from django.conf import settings  # noqa

Thanks for helping me get to that conclusion @mtndesign