[Answered ]-Trying to pass a foreign key of my model into URL for dynamic routing

1👍

You filter on the message__sender, so then it is expecting a User object, or the primary key of a User object.

You can however filter with message__sender__username=username, the same applies for the recepient:

def conversation_view(request, username):
    messages = Message.objects.filter(
        Q(message__sender__username=username) |
        Q(message__recipient__username=username)
    ).order_by('send_time')
    return render(request, "conversation.html", {
        'messages': messages
        })

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Leave a comment