0

I have such a model for representing posts in my system.

class Post(models.Model):
    caption = models.CharField(max_length=256)
    text = models.CharField(max_length=256, null=True, blank=True)
    date_posted = models.DateTimeField(null=True, blank=True)
    source = models.ForeignKey(Source, on_delete=models.CASCADE)
    source_url = models.URLField()
    image = models.ImageField(upload_to='post_images', null=True)

I have a data collector who scrapes data and puts it in this model. The method for inserting scraped single record looks like this:

@staticmethod
def _load_post(post: CollectedPostData, source: Source) -> None:
    print('#' * 70)
    print(source)

    post_obj, created = Post.objects.get_or_create(
        caption=post.caption,
        text=post.text,
        source=source,
        source_url=post.source_url,
        date_posted=parse(post.date_posted) if post.date_posted else None,
    )

    print(post_obj, created)
    print(post.image)

    if created:
        print('CREATED')
        image_content = requests.get(post.image, allow_redirects=True).content
        print(len(image_content))
        post_obj.image.save(post.image, ContentFile(image_content), save=True)

        print('After SAVE')
    print('#' * 70)

When I run the code with Celery, I see that all print statements are executed, there are no issues in logs, all Post objects are created with correct data, but 'post_images' folder in 'media' is not created and zero file created...

The funny thing is that when I am running this code snipped manually all files are created...

I am 'playing' with this for few days and still can't understand why it's not working. Could someone please help?

p.s. I am using:

  • Django==4.1.7
  • Pillow==9.4.0
  • redis==4.5.3

1 Answer 1

0

The solution is here: Saving File in Model in View works but not in Celery Task

(c) I found the issue. The whole App is running in a Docker Environment. The Django app and Celery have been run in separate containers. The problem was that Celery had no access to the Django Filesystem, so the Files got saved, but without reach of the Django Container... Very dumb error, but took me some days figuring out. So if somebody stumbles about the same issues, check your docker volumes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.