Junior — Middle
What architecture pattern is used to organize signals in Django?
sobes.tech AI
Answer from AI
In Django, the Observer pattern is used to organize signals. This pattern allows objects to subscribe to events and receive notifications when they occur. Django signals enable separation of event handling logic from the main business logic, for example, performing actions after saving a model or when it is deleted.
Example of using the post_save signal:
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel
@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, **kwargs):
if created:
print(f'New object created: {instance}')