How-to: Mulitple Objects Serialisation - bulk creation of a list of objects ?
I was lately trying to implement a multi / bulk create Service, similar as described at the DRF. Simple ideas failed, like adding many=True to the class Meta.
https://www.django-rest-framework.org/api-guide/serializers/#dealing-with-multiple-objects
Does DSG support the (bulk) generation of multiple objects, like providing a list of objects to the Create Method ? What is the best way to do that ? Using streams ? I would include it into the DSG Example, once I have an idea.
Merci :)
Hi, there's no builtin way to do that with the generic services but it would be easy to implement. (largely the same as the CreateMixin)
class BulkCreateMixin:
@grpc_action(
request=SelfSerializer,
use_request_list=True,
response=SelfSerializer,
use_response_list=True
)
async def BulkCreate(self, request, context):
serializer = await self.aget_serializer(message=request, many=True)
await sync_to_async(serializer.is_valid)(raise_exception=True)
await self.aperform_create(serializer)
return await serializer.amessage
async def perform_bulk_create(self, serializer):
await serializer.asave()
Merci, @legau, I will try to apply this to the book example in the next days - that we have your "recipe" documented for the public.
btw. @legau, I guess there is a (gRPC) size limit of the length list. So for larger lists, a stream would be necessary ?
Hello @markdoerr
You may want to look at this issue: https://github.com/socotecio/django-socio-grpc/issues/241
and this documentation: https://django-socio-grpc.readthedocs.io/en/stable/features/grpc-action.html#use-request-and-response-list
Also the gRPC size limits is configurable in https://django-socio-grpc.readthedocs.io/en/stable/settings.html#server-options
Thanks, @AMontagu , I did not expect that you can set the size that big of 100MB - I had a size limit of 2-4 MB in my head: (https://github.com/grpc/grpc-web/issues/1182) Increasing this limit would also solve my file-upload issue.... Are there any caveats in increasing the limit (like more overhead...) ?
If I remember correctly the main issue is that bigger message will have impact on your network flow and may increase delay on all your API. But not sure of me.