socotecio / django-socio-grpc

gRPC for Django.
https://django-socio-grpc.readthedocs.io/
Apache License 2.0
104 stars 22 forks source link

Error when trying to use a list request message with a primary related key #252

Open AMontagu opened 11 months ago

AMontagu commented 11 months ago

class DepositImageProtoSerializer(proto_serializers.ModelProtoSerializer):
    deposit = serializers.PrimaryKeyRelatedField(
        queryset=Deposit.objects.all(),
        pk_field=UUIDField(format="hex_verbose"),
        allow_null=True,
        required=False,
    )
    image = Base64ImageField(allow_null=True)

    class Meta:
        model = DepositImage
        proto_class = product_pb2.DepositImageResponse
        proto_class_list = product_pb2.DepositImageListResponse
        fields = [
            "uuid",
            "deposit",
            "image",
        ]

class DepositImageService(generics.AsyncModelService):

    @grpc_action(
        request=[{"name": "deposit", "type": "string"}, {"name": "uuid", "type": "string"}],
        use_request_list=True,
        response=DepositImageProtoSerializer,
        use_response_list=True,
    )
    async def AssignDeposit(self, request, context):
        serializer = DepositImageProtoSerializer(message=request, many=True)
        await serializer.ais_valid()
        print("serializer.data: ", serializer.data)
        await serializer.asave()

        return await serializer.amessage
message DepositImageAssignDepositListRequest {
    repeated DepositImageAssignDepositRequest results = 1;
    int32 count = 2;
}

message DepositImageAssignDepositRequest {
    string deposit = 1;
    string uuid = 2;
}

raise error like:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/services/servicer_proxy.py", line 212, in handler
    response = await safe_async_response(self._middleware_chain, request_container)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/utils/utils.py", line 26, in safe_async_response
    return await response
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/middlewares.py", line 95, in middleware
    return await safe_async_response(get_response, request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/utils/utils.py", line 26, in safe_async_response
    return await response
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/middlewares.py", line 77, in middleware
    return await safe_async_response(get_response, request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/utils/utils.py", line 26, in safe_async_response
    return await response
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/middlewares.py", line 44, in middleware
    return await safe_async_response(get_response, request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/utils/utils.py", line 26, in safe_async_response
    return await response
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/services/servicer_proxy.py", line 171, in _get_response_async
    response = await safe_async_response(wrapped_action, request_container)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_socio_grpc/utils/utils.py", line 26, in safe_async_response
    return await response
           ^^^^^^^^^^^^^^
  File "/opt/code/product/services/depositimage_service.py", line 36, in AssignDeposit
    print("serializer.data: ", serializer.data)
                               ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/rest_framework/serializers.py", line 768, in data
    ret = super().data
          ^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/rest_framework/serializers.py", line 257, in data
    self._data = self.get_initial()
                 ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/rest_framework/serializers.py", line 595, in get_initial
    return self.to_representation(self.initial_data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/rest_framework/serializers.py", line 687, in to_representation
    self.child.to_representation(item) for item in iterable
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/rest_framework/relations.py", line 272, in to_representation
    return self.pk_field.to_representation(value.pk)
                                           ^^^^^^^^
AttributeError: 'str' object has no attribute 'pk'