Reputation: 465
I'm new in Django, maybe my question has a simple answer, but I'm at deadlock.
My signal code lives in signals.py
, I use @receiver
. According to documentations, I imported the signal submodule inside ready() in apps.py
. But it doesn't work =( Could anybody help me?
N.B. If I write my signal code inside models.py
everything works well.
Code:
signal.py
from django.db.models.signals import post_delete
from django.dispatch import receiver
import os
from .models import ProductImage
def delete_image_from_storage(path):
if os.path.isfile(path):
print(path)
os.remove(path)
@receiver(post_delete, sender=ProductImage)
def post_delete_image(sender, instance, **kawargs):
if instance.photo:
print(instance.photo.path)
delete_image_from_storage(instance.photo.path)
apps.py
from django.apps import AppConfig
class ProductsConfig(AppConfig):
name = 'products'
def ready():
import products.signals
settings.py
INSTALLED_APPS = [
...,
'products',
...,
]
Upvotes: 3
Views: 1088
Reputation: 77902
As explained in the FineManual, you need to either explicitely register the appconfig in INSTALLED_APPS
ie:
INSTALLED_APPS = [
...,
'products.apps.ProductConfig',
...,
]
or declare it as default_app_config
in products/__init__.py
:
# products/__init__.py
default_app_config = "products.apps.ProductConfig"
Upvotes: 7