[Answered ]-Django backward foreign key query not working

1👍

You have specified that related_name quite incorrectly.
(With your current configuration, you could do user.receiver.all() to get received messages, but that makes little sense for a reader.)

You’ll want something like

sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="messages_sent")
receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name="messages_received")

after which you can use user.messages_received.all() and user.messages_sent.all().

👤AKX

Leave a comment