简体   繁体   中英

can't import django model into celery task

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM