Home > front end >  Django: send notifications at user based on database change
Django: send notifications at user based on database change

Time:06-29

I have 3 database tables:

  • users (stores info about users e.g. email, name)
  • metadata (stores data)
  • activity (stores changes made to users/metadata tables)

I want to achieve the following:

  • to store any change into the activity table (e.g. new user is created, a user updates the metadata table)
  • to send notifications to users whenever a change into the users/metadata tables happens.

What are the libraries/method that I could use in order to achieve the desired functionalities? Thank you!

CodePudding user response:

in addition to django signals which wes already recommended, you can also check out django channels django activity stream

CodePudding user response:

Assuming you are making use of the Django Models, I would use the Django Signals for saving the data to a second model: "MetaData" according to your question.

Specifically the post_save signal:

https://docs.djangoproject.com/en/4.0/ref/signals/#django.db.models.signals.post_save

@receiver(post_save, sender=User)
def post_save_user(sender, instance, created, **kwargs):
    # Save Stuff To Metadata Model.
    meta = MetaData(user=instance)
    meta.save()

You need to import those signals inside your models.py placed at the bottom of the file.


As for the notifications, I would follow @kristina's advice and use Django Channels.

Just remind that your application needs to be ASGI (Async). So your application server needs to be one like uvicorn, daphne, hypercorn, etc.

  • Related