[英]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. django.core.exceptions.ImproperlyConfigured:请求设置DEFAULT_INDEX_TABLESPACE,但未配置设置。 You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. 您必须在访问设置之前定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。
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? 如何将django模型导入芹菜任务?
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 感谢您帮助我得出结论@mtndesign
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.