Cannot 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: the requested setting is DEFAULT_INDEX_TABLESPACE, but the settings are not configured. You must either define the DJANGO_SETTINGS_MODULE environment variable or call settings.configure () before accessing the settings.

If I take out an entity object, all things are beautiful and no errors occur. When adding back:

 from myapp.models import Entity 

The error is returned.

 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 to import django model into celery task?

+5
source share
1 answer

My celery file should 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 come to this conclusion @mtndesign

+6
source

Source: https://habr.com/ru/post/1247557/


All Articles