Has*_*ani 4 python django django-serializer grpc grpc-python
我有 django REST API,我正在尝试将其转换为 gRPC。我遵循Django grpc 框架指南并创建了以下文件:
class Organization(models.Model):
Org_name = models.CharField(max_length=100, unique=True, primary_key=True, db_index=True)
Address = models.CharField(max_length=100)
Description = models.CharField(max_length=500)
Number_of_emp = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
package org;
import "google/protobuf/empty.proto";
service OrganizationController {
rpc List(OrganizationListRequest) returns (Organizations) {}
rpc Create(Organization) returns (Organization) {}
rpc Retrieve(OrganizationRetrieveRequest) returns (Organization) {}
rpc Update(Organization) returns (Organization) {}
rpc Destroy(Organization) returns (google.protobuf.Empty) {}
}
message Organization {
string Org_name = 1;
string Address = 2;
string Description = 3;
int32 Number_of_emp = 4;
}
message OrganizationListRequest {
}
message OrganizationRetrieveRequest {
string Org_name = 1;
}
message Organizations {
repeated Organization organization = 1;
}
Run Code Online (Sandbox Code Playgroud)
请注意,Organizations 是 org.proto 中声明的消息,用于返回 List 或对象数组
class OrganizationService(generics.ModelService):
queryset = Organization.objects.all()
serializer_class = OrganizationSerializerProto
Run Code Online (Sandbox Code Playgroud)
class OrganizationSerializerProto(proto_serializers.ModelProtoSerializer):
class Meta:
model = Organization
proto_class = org_pb2.Organization
fields = '__all__'
Run Code Online (Sandbox Code Playgroud)
问题我想使用 发出请求来rpc List(OrganizationListRequest) returns (Organizations) {}获取数据库中所有组织的列表。但是,每当我调用 rpc 时,都会收到以下错误:
请求错误:“错误”:“13 内部:无法序列化响应!” (我使用 BloomRPC gui 客户端来发出请求)
但是,如果我更改
rpc List(OrganizationListRequest) returns (Organizations) {}为
rpc List(OrganizationListRequest) returns (stream Organization) {}
请求,则返回工作正常,并且我会得到包含所有对象的响应流。我不想在流中返回数据,我希望它返回对象数组,但使用message Organizations {repeated Organization organization = 1;}会引发上述错误。我犯错了吗?我到处搜索但找不到这个错误。或者说不可能像这样进行RPC调用?
这是服务器端原始编解码器错误。发生这种情况通常是因为:
Organizations而不是Organization)。gRPC Python 将记录导致错误的异常Failed to serialize response!。您可以通过 显式启用 Python 日志记录logging.basicConfig(level=logging.INFO)。这是日志记录行的链接:
如果经过上述检查后,错误仍然存在,您可以创建一个重现案例并将其发送到https://github.com/grpc/grpc/issues吗?