Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How-to: Mulitple Objects Serialisation - bulk creation of a list of objects ? #259

Open
markdoerr opened this issue Jan 30, 2024 · 6 comments
Assignees

Comments

@markdoerr
Copy link
Contributor

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 :)

@legau
Copy link
Contributor

legau commented Jan 31, 2024

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()

@markdoerr
Copy link
Contributor Author

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.

@markdoerr
Copy link
Contributor Author

btw. @legau,
I guess there is a (gRPC) size limit of the length list. So for larger lists, a stream would be necessary ?

@AMontagu
Copy link
Collaborator

@markdoerr
Copy link
Contributor Author

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:
(grpc/grpc-web#1182)
Increasing this limit would also solve my file-upload issue....
Are there any caveats in increasing the limit (like more overhead...) ?

@AMontagu
Copy link
Collaborator

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.

@AMontagu AMontagu self-assigned this Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants