How can we send a Django User object and a list of users?

171 views
Skip to first unread message

Rajeev R

unread,
Mar 17, 2017, 12:08:22 PM3/17/17
to grpc.io
Hello All,

I have two django applications and I want to send a user object from one django application to other django application through gRPC. I have seen string and integer types working fine. Please find the code attached.



service Usermanagement {
  rpc
UserLogin (UserRequest) returns (UserResponse) {}
}

// The request message containing the user's name.
message
UserRequest {
 
string username = 1;
 
string password = 2;
}

// The response message containing the greetings
message
UserResponse {
 
string message = 1;
 
string fullname = 2;
 
string token = 4;
  int32 status
= 5;
}

This is my Django application code.

import grpc

from userservice import userservices_pb2
from userservice import userservices_pb2_grpc
from usermanagement.models import AppUser as User
from django.contrib.auth import authenticate
from rest_framework.authtoken.models import Token
from google.protobuf import any_pb2

class Usermanagement(userservices_pb2_grpc.UsermanagementServicer):

   
def UserLogin(self, request, context):
       
User = authenticate(username=request.username, password=request.password)
       
if User is not None:
           
(token, created) = Token.objects.get_or_create(user=User)
           
User.authentication_token = token.key
           
return userservices_pb2.UserResponse(message='success', status=0, fullname=User.fullname, token = token.key)
       
else:
           
return userservices_pb2.UserResponse(message='Incorrect Username/Password', status=1)


Please anyone can help me "How can I include the User object or user list" in the return response.

Rajeev R

unread,
Mar 20, 2017, 5:18:39 AM3/20/17
to grpc.io
message UserResponse {
 
string message = 1;
 
string fullname = 2;
 
string token = 4;
  int32 status
= 5;
}


message
UserListRequest {
   
}

message
UserListResponse {
    message
Result {
   
string fullname = 1;
 
}
  repeated
Result results = 1;
}
in my django application code,

    def UserList(self, request, context):
       
UserListRes = User.objects.all().values('fullname')
       
return userservices_pb2.UserListResponse(results=UserListRes)

jome.ak...@gmail.com

unread,
Jun 10, 2017, 6:18:57 AM6/10/17
to grpc.io
Hi. Please could you tell me how you got the grpc server to run with the django process?
Reply all
Reply to author
Forward
0 new messages