Amit Pal
Amit Pal

Reputation: 11052

How to call a method in views.py automatically after updating/modifying the table in Django?

I am building a web based application, in which i need to send the notification to registered user by E-mail after updating or modifying some value in tables.

I have a model.py:

class ProileComment(models.Model):
    comment = models.TextField()
    user = models.CharField(max_length=30, null=False, blank=True)
    timestamp = models.DateTimeField(null=False, blank=True)

Method in views.py

def send_email(request):
   ##

According to my problem if ProfileComment model will update then it should automatically called the send_email method. So that user will get the notification about the changes in database. How should i proceed?

Upvotes: 0

Views: 84

Answers (1)

dm03514
dm03514

Reputation: 55962

You can use django signals!!

https://docs.djangoproject.com/en/dev/ref/signals/

They allow you to register a signal listener. perhaps pre_save signal would best suite your needs? Whenever something is saved anywhere in your app your signal will be called, and you can make a decision based on the model or whatever other conditions you need.

Or if you only want to send_email for that one model only you could override the save method!

class ProfileComment(models.Model):


    def save(self, *args, **kwargs):
      # send email?
      super(ProfileComent, self, *args, **kwargs) # make sure to call parent!

Upvotes: 1

Related Questions