(Django model) How does this Message model works?

So I am following a Youtube video on how to create a chatting app. Then it build a model that I don't understand. Here's the Message model I came across and can't understand how it works.

class Message(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user')
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='from_user')
    recipient = models.ForeignKey(User, on_delete=models.CASCADE, related_name='to_user')
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    is_read = models.BooleanField(default=False)

    def send_message(from_user, to_user, body):
        sender_message = Message(user=from_user, sender=from_user, recipient=to_user, body=body, is_read=True)

        recipient_message = Message(user=to_user, sender=from_user, recipient=from_user, body=body, is_read=True)

        return sender_message

    def get_message(user):
        users = []
        messages = Message.objects.filter(user=user).values('recipient').annotate(last=Max('date')).order_by('-last')
        # filter by user=the login user, recipient=the sender, the lastest message from each sender, order the lastest message by sender using time

        for message in messages:
                'user': User.objects.get(pk=message['recipient']),
                'last': message['last'],
                'unread': Message.objects.filter(user=user, recipient__pk=message['recipient'], is_read=False).count(),

        return users

I understand the different fields of the Message model but I fail to understand why it create two instances of the message model in the send_message() function. One for sender message and another for recipient message.

recipient_message = Message(user=to_user, sender=from_user, recipient=from_user, body=body, is_read=True)

Then for the recipient_message I'm not clear why the recipient field is set to from_user instead of to_user??

Could anyone please help me with this?

I am in confusion how this Message model works.

So to answer your first question, two models are created because none of the fields in the model are unique, when you instantiate a new instance of the Message class you are essentially instantiating two separate rows in the database table.

It's unclear why the user field is flipped but it's likely done so a user can filter messages for themselves, though these are both poor practices.

The Message model has fields to store information about the message, including the sender and recipient (both of which are foreign keys to the User model), the message body, and the date the message was sent.

The model also has two methods: send_message and get_message.

The send_message method creates two instances of the Message model - one for the sender and one for the recipient. It saves both instances to the database and returns the sender's message.

The get_message method retrieves messages for a specific user and returns a list of dictionaries, each containing information about the recipient of the message, the date of the last message received, and the number of unread messages from that recipient. This information is retrieved using the Django ORM's filter and annotate methods, which allow for querying the database and aggregating data in a flexible way.

Back to Top