I am trying to add simple user to user messaging functionality to my blog app in Django. I have a view that allows a user to send a message but I am having difficulty in displaying the messages after. Here is my Message model:
class Message(models.Model):
user = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='messages')
sender = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='messages_from')
message = models.TextField(blank=True)
timestamp = models.DateTimeField(default=timezone.now, editable=False)
unread = models.BooleanField(default=True)
And the related Profile model:
class Profile(models.Model):
first_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
slug = models.SlugField(unique=True, blank=True)
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
Here is my MessagesView attempt:
class MessagesView(ListView):
model = Profile
template_name = 'users/messages.html'
context_object_name = 'msg'
def get_queryset(self):
return Profile.objects.filter(messages_from__isnull=False, messages__user_id=self.request.user)
And here is the url:
path('messages/<int:pk>/', MessagesView.as_view(), name='messages'),
Upon trying to load this page I receive the error Cannot query "user": Must be "Profile" instance where "user" is the name of the logged in user who's messages I am attempting to load. I have spent a long time googling a solution to this but have found nothing that relates to my case. Please help
Your
Messagehas asuserfield aForeignKeytoProfile, notUser(therefore it might be better to rename the field toprofile). This thus means that filtering likemessages__user_id=self.request.userdoes not make much sense.You can filter by following the relation from
ProfiletoUserwith:The
.distinct()[Django-doc] is necessary here to prevent retrieving the sameProfilemultiple times.Since this is a
MessagesView, you furthermore probably should returnMessages, the fact that you set thecontext_object_nametomsgalso hints to that. In that case you thus should return aQuerySetofMessages, notProfiles:In case you rename the
ForeignKeytoprofile, you filter withprofile__user=self.request.user.