Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:27:19+00:00 2026-05-26T18:27:19+00:00

In my models.py: from django.db import models from core import tasks class Image(models.Model): image

  • 0

In my models.py:

from django.db import models
from core import tasks

class Image(models.Model):
    image     = models.ImageField(upload_to='images/orig')
    thumbnail = models.ImageField(upload_to='images/thumbnails', editable=False)

    def save(self, *args, **kwargs):
       super(Image, self).save(*args, **kwargs)
       tasks.create_thumbnail.delay(self.id)

In my tasks.py:

from celery.decorators import task
from core.models import Image

@task()
def create_thumbnail(image_id):
    ImageObj = Image.objects.get(id=image_id)
    # other stuff here

This is returning the following:

  • Exception Type: ImportError
  • Exception Value: cannot import name tasks

The error disappears if I comment out from core.models import Image in tasks.py, however this obviously will cause a problem since Image has no meaning in here. I have tried to import it inside create_thumbnail however it still won’t recognize Image.

I have read somewhere that usually the object itself can be passed as an argument to a task and that would solve my problem. However, a friend once told me that it is considered best practice to send as little data as possible in a RabbitMQ message, so to achieve that I’m trying to only pass the image ID and then retrieve it again in the task.

1) Is what I’m trying to do considered a best practice? If yes, how do I work it out?

2) I have noticed in all the examples I found around the web, they execute the task from a view and never from a model. I’m trying to create a thumbnail whenever a new image is uploaded, I don’t want to call create_thumbnail in every form/view I have. Any idea about that? Is executing a task from a model not recommended or a common practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:27:20+00:00Added an answer on May 26, 2026 at 6:27 pm

    1) Is what I’m trying to do considered a best practice? If yes, how do I work it out?

    Yes, passing only a little information to the task is generally a good thing like you mentioned.

    2) I have noticed in all the examples I found around the web, they execute the task from a view and never from a model. I’m trying to create a thumbnail whenever a new image is uploaded, I don’t want to call create_thumbnail in every form/view I have. Any idea about that? Is executing a task from a model not recommended or a common practice?

    I’ve noticed the same thing, and feel that tutorials and documentation call tasks from their views because it is easier to demonstrate how things work using simple views than with models or forms.

    To eliminate circular imports, you should think about which way the imports should happen. Generally, tasks.py will need to import many things from models.py whereas models.py rarely needs to know anything about tasks.py. The standard should be that models.py does not import from tasks.py. Thus, if you do need to do this and are calling a task from a model method, make the import in the method as so:

    from django.db import models
    
    class Image(models.Model):
        image     = models.ImageField(upload_to='images/orig')
        thumbnail = models.ImageField(upload_to='images/thumbnails', editable=False)
    
        def save(self, *args, **kwargs):
           super(Image, self).save(*args, **kwargs)
           from core.tasks import create_thumbnail
           create_thumbnail.delay(self.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: from django.db import models class Food(models.Model): Food, by name. name = models.CharField(max_length=25) class
Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it
Given a Django model likeso: from django.db import models class MyModel(models.Model): textfield = models.TextField()
This is my models.py from django.db import models class School(models.Model): school = models.CharField(max_length=300) def
I have a model project_phase : from django.db import models from django.utils import simplejson
I have the following models: http://slexy.org/view/s20T8yOiKZ from mxutils.cms_services import generate_secid from django.db import models
from django.contrib.auth.models import User u = User.objects.get(username='test') user.password u'sha1$c6755$66fc32b05c2be8acc9f75eac3d87d3a88f513802 Is reversing this password encryption
I've got a couple django models that look like this: from django.contrib.sites.models import Site
I have installed django-registration with pip, version 0.7. Then with urls.py from core.forms import
I have written the following custom field: from django.core.urlresolvers import reverse from django.db import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.