I am using signals in my Blog model and trying to compress and resize image but image compressing not working in signals here is my code:
class Blog(models.Model): #my this model using signals
author = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,max_length=100, related_name='author')
blog_cover_image = models.ImageField(upload_to='blog/images/',validators=[validate_file_size,FileExtensionValidator( ['png','jpg'] )],blank=True,null=True)
def save(self,*args,**kwargs):
super().save(*args, **kwargs)
img = Image.open(self.blog_cover_image.path)
if img.height > 300 or img.width > 300:
out_put_size = (300,300)
img.thumbnail(out_put_size)
img.save(self.blog_cover_image.path)
#my others model fields..
post_save.connect(Blog.blog_notify, sender=Blog)
I tested this code on my another model which not using signals and it worked but why image compressing not working in my Blog model which using django post_save signals ?
Hi @MDFARHYN, have you verified that img.save
gets run? Have you check to see if it is saving the compressed file, but giving it a new name or saving it to an unexpected location?
1 Like
CodenameTim I tried exactly same code on my another model which not using signals and it worked but I am not understanding why it’s not working in signals
I’m not sure how much image processing you’re doing, but there are a few libraries out there that could manage this for you. I believe django-imagekit would be a good fit for this use-case from what I can see.
1 Like
Can you describe what is happening and what is not happening when you say “it’s not working in signals”?
1 Like
I am not seeing any errors in my terminal and it’s not resizing image for Blog model.
Thank you. I can’t tell what might be wrong by looking at the code above. Answering the following questions will narrow down the problem:
- Does the
img.thumbnail
function get called? If so does img.thumbnail
work as expected?
- Does saving the compressed file give it a new filepath/name?
1 Like
CodenameTim This is my another model where I am using same code except not using signals. the image compression and resizing working in this mode.
class BlogHeaderImage(models.Model):
image = models.ImageField(upload_to='blog/images/',validators=[validate_file_size,FileExtensionValidator( ['png','jpg'] )])
blog = models.ForeignKey(Blog,on_delete=models.CASCADE,related_name="blog_header_image")
def save(self,*args,**kwargs):
super().save(*args, **kwargs)
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
out_put_size = (300,300)
img.thumbnail(out_put_size)
img.save(self.image.path)
if exactly same code working in this model then why it’s not working in my Blog model? any idea? I tried after removed signals in my blog model then it’s working but why it’s not working when using signals???
Have you tried connecting an empty receiver and seeing if the problem still occurs? If it doesn’t occur, then the issue is in blog_notify
.
1 Like
CodenameTim no I am using sender and receiver both in my siganls
I meant rather than specifying Blog.blog_notify
, create a function that does nothing and use that as the receiver.
1 Like
CodenameTim I am not understanding how to create function that as receiver.
here is my full code given:
class Blog(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,max_length=100, related_name='author')
blogcoverimage = models.ImageField(upload_to='blog/images/',validators=[validate_file_size,FileExtensionValidator( ['png','jpg'] )],blank=True,null=True)
def save(self,*args,**kwargs):
super().save(*args, **kwargs)
img = Image.open(self.blogcoverimage.path)
if img.height > 300 or img.width > 300:
out_put_size = (300,300)
img.thumbnail(out_put_size)
img.save(self.blogcoverimage.path)
title = models.CharField(max_length=300,unique=True)
#my others models fields
#here signals stating.....
def blog_notify(sender, instance, *args, **kwargs):
blog = instance
blog_title = blog.title
sender =blog.author
receiver = blog.author
if sender == blog.author and blog.is_published == "published":
notify = Notifications(blog=blog, sender=sender, receiver=receiver,text_preview = blog_title[:250], notification_type="post approved",duplicate_value="author")
notify.save()
post_save.connect(Blog.blog_notify, sender=Blog)
where I am doing mistake? I am using signals only pass few text and user info to my another model which name is Notifications. It seems to me it’s have no relation with my blogcoverimage fields.
Hi @MDFARHYN, here’s what I had in mind:
class Blog(models.Model):
...
def receiver_that_does_nothing(*args, **kwargs):
pass
post_save.connect(receiver_that_does_nothing, sender=Blog)
# Comment out the existing signal so it's not wired up.
# post_save.connect(Blog.blog_notify, sender=Blog)
Unfortunately, I can’t see any glaring mistakes in the code. It requires debugging and eliminating possibilities. Can you step through this code using a debugger? Alternatively, you can put in print()
statements to track the execution and see where things break or run differently.
CodenameTim Though I tried your solution but didn’t work and I can’t use this solution because I need to be pass my blog_notify function which is very important for me.
I am sue the saving function not calling for blogcoverimage but not understanding why it’s not calling. I tried print("###image saving")
inside if statement and didn’t see the text “image saving” in my console which confirm me save function not calling. It’s also weird for me because it’s seems to me there is not mistake in my code but not understanding why save function not calling?
CodenameTim I find out out my mistake I was try to using using two save function in my model then I moved my blogcoverimage issue to my old save function and it’s working. From this mistake I learned we can’t use two or multiple save function like this in our model:
#my new save function
def save(self,*args,**kwargs):
#using for my image save
# my old save function
def save(self,*args,**kwargs):
#using for my another fileds
The problems solved after using one save function for my all task.
now using only one save function for all
def save(self,*args,**kwargs):
#using for my another fileds
#using for my image fields
So we can’t use multiple save function inside our model. CodenameTim also thanks for your suggestion.