gpt4 book ai didi

python - 无法将 Django 模型导入 celery 任务

转载 作者:太空狗 更新时间:2023-10-29 23:57:55 26 4
gpt4 key购买 nike

我有以下任务:

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: 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.

如果我取出实体导入,一切都很好,不会发生错误。加回来的时候:

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 模型导入 celery 任务?

最佳答案

我的 celery 文件需要有:

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 帮我得出结论

关于python - 无法将 Django 模型导入 celery 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36755583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com