I had learn about django signals but, I don't known where to implement in my project and how to use it in my project. In my project I want send email alerts if it matches to some particular criteria. In this case I need use post_save signals.I added the code with this. Kindly share your ideas.
models.py
class Personal(models.Model):
user = models.OneToOneField(User)
email = models.EmailField(max_length=100, blank=True, null=True)
country = models.CharField(max_length=100, blank=True, null=True)
state = models.CharField(max_length=100, blank=True, null=True)
city = models.CharField(max_length=100, blank=True, null=True)
class Skills(models.Model):
user = models.ForeignKey(User)
skill = models.CharField(max_length=100, blank=True, null=True)
class jobs(models.Model):
emp = models.ForeignKey(User, unique=False)
title = models.CharField(max_length=100)
industry = models.CharField(max_length=100)
functionalarea = models.CharField(max_length=100)
min_exp = models.IntegerField(default=0)
max_exp = models.IntegerField(default=0)
city = models.CharField(max_length=100)
state = models.CharField(max_length=100)
country = models.CharField(max_length=100)
empskills = models.CharField(max_length=100, blank=True, null=True)
Here I want to match city and empskills of jobs table with personal.city and Skills.skill model. This event occurs every job posting if it matches any one field it will send mail to personal.email automatically. Please give sample one where and how to use the signal.
There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown.
Post-save SignalThe post_save logic is just a normal function, the receiver function, but it's connected to a sender, which is the Order model. The code block below demonstrates the sample receiver function as a post-save. 1from django. db. models.
pre_save) is provoked just before the model save() method is called, or you could say model save method is called only after pre_save is called and done its job free of errors.
Django Signals - post_delete()To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.
Write a function outside the model that handles when a new job is posted and finds the people to email.
You then specify Job
as the sender
for a post_save
signal and connect the function.
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=Job)
def handle_new_job(sender, **kwargs):
job = kwargs.get('instance')
logger.info("POST_SAVE : Job : %s" % job)
# find people to email based on `job` instance
This is an example of how to use post_save
from django.db.models.signals import post_save
from .models import MyModel
def my_handler(**kwargs):
if kwargs[’raw’]:
return
...
# here you could return your mails
post_save.connect(my_handler, sender=MyModel)
Hope this could help you !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With