Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 735497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:29:50+00:00 2026-05-14T07:29:50+00:00

Trying to add email notification to my app in the cleanest way possible. When

  • 0

Trying to add email notification to my app in the cleanest way possible. When certain fields of a model change, app should send a notification to a user. Here’s my old solution:

from django.contrib.auth import User

class MyModel(models.Model):
    user = models.ForeignKey(User)
    field_a = models.CharField()
    field_b = models.CharField()

    def save(self, *args, **kwargs):
        old = self.__class__.objects.get(pk=self.pk) if self.pk else None
        super(MyModel, self).save(*args, **kwargs)
        if old and old.field_b != self.field_b:
            self.notify("b-changed")
        # Sevelar more events here
        # ...

    def notify(self, event)
        subj, text = self._prepare_notification(event)
        send_mail(subj, body, settings.DEFAULT_FROM_EMAIL, [self.user.email], fail_silently=True)

This worked fine while I had one or two notification types, but after that just felt wrong to have so much code in my save() method. So, I changed code to signal-based:

from django.db.models import signals

def remember_old(sender, instance, **kwargs):
    """pre_save hanlder to save clean copy of original record into `old` attribute
    """
    instance.old = None
    if instance.pk:
        try:
            instance.old = sender.objects.get(pk=instance.pk)
        except ObjectDoesNotExist:
            pass

def on_mymodel_save(sender, instance, created, **kwargs):
    old = instance.old
    if old and old.field_b != instance.field_b:
        self.notify("b-changed")
    # Sevelar more events here
    # ...

signals.pre_save.connect(remember_old, sender=MyModel, dispatch_uid="mymodel-remember-old")
signals.post_save.connect(on_mymodel_save, sender=MyModel, dispatch_uid="mymodel-on-save")

The benefit is that I can separate event handlers into different module, reducing size of models.py and I can enable/disable them individually. The downside is that this solution is more code and signal handlers are separated from model itself and unknowing reader can miss them altogether. So, colleagues, do you think it’s worth it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:29:50+00:00Added an answer on May 14, 2026 at 7:29 am

    I think it’s a good idea. The “Custom Signals for Uncoupled Design” talk from the most recent DjangoCon is a great resource of what is possible and appropriate with signals in Django.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add email functionality to my app. I can get the
I'm trying to manually add an individual email error message to my model but
In my app, I want to add a user notification each time a user
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a blank sample app for a rails tutorial to GitHub, but
I'm trying to add an attachment to my email from a browse button in
I'm trying to add a remember me feature to my web app to let
I am trying to send a simple email through the following code: protected void
I'm trying to add incoming email to my web application. It's built on CodeIgniter
I am trying to configure Jira from email section of email notification. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.