不能将django模型导入芹菜任务

时间:2016-04-20 21:31:25

标签: python django celery django-celery

我有以下任务:

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

我收到以下错误:

  

django.core.exceptions.ImproperlyConfigured:请求的设置   DEFAULT_INDEX_TABLESPACE,但未配置设置。你必须   要么定义环境变量DJANGO_SETTINGS_MODULE,要么调用   在访问设置之前进行settings.configure()。

如果我取出实体导入,那么每件事情都没有问题,也就不会发生错误。加回时:

from myapp.models import Entity

错误返回。

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

如何将django模型导入芹菜任务?

1 个答案:

答案 0 :(得分:6)

我的芹菜文件需要:

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

感谢您帮助我得出结论@mtndesign