0

I am kinda stuck while I am working on a Django project (creating and scheduling a meeting). I need your help!

For the project, I want to send notification (likely pop-up one since I am not implementing email when registering) to users that were selected by the meeting creator. They will receive the notification when they first log in. I have looked at messaging frameworks of Django but it seems like they don't have the method that I am looking for. I am also very new to Django so I really need your help! Any suggestions would be appreciated!

1 Answer 1

2

You'll need a model that identifies the notification.

class Notification(models.Model):
    created = models.DateTimeField(auto_add_now=True)
    updated = models.DateTimeField(auto_now=True)
    text = models.TextField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    read = models.DateTimeField(null=True, blank=True)

You can create however many notifications for a user where ever in your application.

If you need to show them on any page, it'd be best to pull them in a context processor. If it's only on a specific page, I'd add them to the template rendering context for that view.

As for marking them read you have a decision to make. You can elect to mark them read when they are rendered to the user which would happen at the same time as you pull them in either of the above situations. Or you can force the user to dismiss them and then mark it as read. If you do that you'll need an ajax view that takes a notification id or some other identifier, verifies the requesting user matches the notification's user and set read=django.utils.timezone.now()

Edit:

Re-reading the question, here's a more appropriate answer:

class Notification(models.Model):
    created = models.DateTimeField(auto_add_now=True)
    text = models.TextField()
    users = models.ManyToManyField(User, through='NotificationUser')

class NotificationUser(models.Model):
    created = models.DateTimeField(auto_add_now=True)
    updated = models.DateTimeField(auto_now=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    notification = models.ForeignKey(User, on_delete=models.CASCADE)
    read = models.DateTimeField(null=True, blank=True)

This way you can have a single notification instance with it's properties like text (or maybe creator). And then have the notification-user specific properties like whether that particular user has read/seen the notification. Edit: I re-read your issue and it says to send a notification to multiple users. In that case something like this may be better.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! so then, do I use the message method (it's info one I guess) to display the message?
No. You'd need to create something new.
Do you have a link so I can look into detail? Sorry I am really new to Django! :/
What’s the way to send specific users a pop-up notification when an user submit the form?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.