Wednesday, 12 April 2023

Issue with file write using Django and Celery

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


from Issue with file write using Django and Celery

No comments:

Post a Comment