1

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!

2
  • I have not used it, however github.com/django-notifications/django-notifications looks like it may be what you're looking for. Commented Apr 8, 2021 at 7:50
  • Thank you, I'll have a look at it and see if I can use it :) Commented Apr 8, 2021 at 7:57

2 Answers 2

1

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.

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

Comments

0

You could use django-notifications for this, you could read aboutt it from their documentation. https://github.com/django-notifications/django-notifications

2 Comments

I implemented something similar to this in a social media website i built
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

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.