4

I am working with signals in Django, specifically with the pre_delete signal, code:

signals.py file:

def delete_unnecessary_image(sender, instance, using):
    print('holaaaa')
    instance.image.delete(save = False) 

apps.py file (here I register the receivers in the ready() method):

def ready(self):
    from .signals import delete_unnecessary_image
    from .models import Course
    pre_delete.connect(delete_unnecessary_image, sender = Course)

My INSTALLED_APPS and configuration in my settings.py:

DJANGO_APPS = [
    ....
]

MY_APPS = [
    'core',
    'courses',
    'instructor'
]

INSTALLED_APPS = DJANGO_APPS + MY_APPS

The problem is that it doesn't work, it doesn't eliminate the image of the Course model, and I don't know why. Any solution?

Julio Cesar
  • 255
  • 5
  • 17
  • Please show us your `INSTALLED_APPS` setting. – solarissmoke Aug 12 '19 at 03:36
  • What type of field is `image` on the `Course` model? What does `save=False` mean in the call to `instance.image.delete(save=False)`? – azundo Aug 12 '19 at 04:55
  • the `save=False` means that it will not save the file or image when removing the value from the field. and in the model the `image` field is a `ImageField`. – Julio Cesar Aug 12 '19 at 05:13
  • @solarissmoke I already updated my answer showing my `INSTALLED_APPS` – Julio Cesar Aug 12 '19 at 05:23
  • I think you're probably running into this issue: https://stackoverflow.com/questions/37429726/overriding-appconfig-ready/37430196#37430196 . Once you fix that, you will get another error which is that `save` is an invalid kwarg for `delete()` - it doesn't make sense to pass `save=False` here. – solarissmoke Aug 12 '19 at 08:20
  • In the django documentation it says that if it is necessary to specify the `save=False` for the file to be deleted, check it out: https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.fields.files.FieldFile.delete – Julio Cesar Aug 12 '19 at 16:52
  • My problem is already solved thanks! by the way I did not get error for the `save=False` – Julio Cesar Aug 12 '19 at 16:55
  • where should i but the `save=False` – Ali Husham Jun 28 '21 at 10:59

0 Answers0