Reputation: 57
so I am building the backend for a web forum using Django & Django REST Framework and would like to include a notification system.
So I have a "Discussion" Model:
def user_directory_path(instance, filename):
return f'{instance.id}/{filename}'
class Discussion(models.Model):
title = models.CharField(max_length=250)
content = models.TextField(max_length=10000)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
is_pinned = models.BooleanField(default=False)
is_open = models.BooleanField(default=True)
is_boosted = models.BooleanField(default=False)
started_by = models.ForeignKey(to=User, related_name='started_discussions', on_delete=models.CASCADE)
subscribers = models.ManyToManyField(to=User, related_name='subscribed_discussions')
tools = models.ManyToManyField(to=Tool, related_name='belongs_to_channels', blank=True)
channels = models.ManyToManyField(to=Channel, related_name='discussions')
def __str__(self):
return f'{self.id} | By: {self.started_by.username} | Content: {self.title} ...'
and I have a "Comment" Model:
class Comment(models.Model):
content = models.CharField(max_length=5000)
discussion = models.ForeignKey(to=Discussion, related_name='comments', on_delete=models.CASCADE)
written_by = models.ForeignKey(to=User, related_name='written_comments', on_delete=models.CASCADE)
liked_by = models.ManyToManyField(to=User, related_name='liked_comments', blank=True)
def __str__(self):
return f'{self.id} | By: {self.written_by.username} | {self.content[0:30]} ...'
Basically my question is that I would like to create a functionality where if someone creates a new comment on a discussion, the users that have subscribed to this discussion will get a notification. So for example if someone comments on a discussion you are subscribed to, you'll get a notification "New Comment from {user that commented} on {discussion you're subscribed to}"
My thought was to create a new django app "Notification" with the respective model and serializer to it.. But I'm really not sure how to get started or whats the best way to implement this?
Hopefully I was able to provide enough information for someone to answer and thanks a lot in advance!
Upvotes: 1
Views: 1121
Reputation: 1
You could use django-notifications for this, you could read aboutt it from their documentation. https://github.com/django-notifications/django-notifications
Upvotes: 0
Reputation: 131
You should look into Django channels and websockets. When you connect to a website or make a request to it your connection to the website is actually cut short, a websocket connection keeps the TCP connection open which is perfect for sending events down to a client from the server when a specific event happens.
Django channels are Django's implementation of websockets, in Node for example uses socket.io.
I haven't used the channels in any project yet so I don't want to mislead you with something I don't know works myself but I know that what your looking for here is websockets.
Upvotes: 1