Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django how to use post_save signals

Tags:

django

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.

like image 710
user Avatar asked Aug 09 '13 05:08

user


People also ask

How do you use signals in Django?

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.

What is Post_save in Django?

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.

What is pre save signal in Django?

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.

What is the use of the Post_delete signal in Django?

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.


2 Answers

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
like image 80
Emil Davtyan Avatar answered Oct 01 '22 12:10

Emil Davtyan


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 !

like image 35
drabo2005 Avatar answered Oct 01 '22 10:10

drabo2005