我有以下任务:
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.不正确配置:请求的设置 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 模型导入到芹菜任务中?
我的芹菜文件需要有:
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