My goal is to redirect the client to the chat session detail view if they are trying to open a new chat session with someone they already have a chat session with. Everything works fine but when I tried to open a duplicate chat session, it didnt get redirected and all what i got is a blank json response
//views.py
class ChatSessionListView(generics.ListCreateAPIView):
serializer_class = ChatSessionSerializer
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
return ChatSession.objects.filter(Q(initiator=self.request.user) | Q(receiver=self.request.user))
def perform_create(self, serializer):
receiver_username = self.request.data['username']
receiver = get_object_or_404(User, username=receiver_username)
chat_session = ChatSession.objects.filter(Q(initiator=self.request.user, receiver=receiver) | Q(initiator=receiver, receiver=self.request.user))
if chat_session.exists():
return redirect('v1:chat:chat_session_detail', session_id=chat_session[0].pk)
else:
serializer.save(initiator=self.request.user, receiver=receiver)
// urls.py
urlpatterns = [
path('', views.ChatSessionListView.as_view(), name='chat'),
path('<str:session_id>/', views.ChatSessionDetailView.as_view(), name='chat_session_detail')
]