alanjds / drf-nested-routers

Nested Routers for Django Rest Framework
https://pypi.org/project/drf-nested-routers/
Apache License 2.0
1.68k stars 160 forks source link

Error when `request.data` is a list in `NestedViewSetMixin` #349

Open cotyq opened 5 months ago

cotyq commented 5 months ago

When using NestedViewSetMixin with request.data as a list, the method initialize_request raises a TypeError.

TypeError: list indices must be integers or slices, not str

Cause

The error occurs because the method assumes request.data is always a dictionary, but in cases where the request contains a list, this assumption fails.

Steps to reproduce

  1. Define a view using NestedViewSetMixin and ModelViewSet.
  2. Create an action that receives a list in the request data.
  3. Send a POST request to the action with a list in the payload.

Example:

class NestedClass(NestedViewSetMixin, ModelViewSet):
    parent_lookup_kwargs = ...
    queryset = ...
    serializer_class = ...

    @action(detail=False, methods=["post"])
    def some_action(self, request, *args, **kwargs):
       # request.data is a list
       ...

Possible Fix

A possible fix to this could be to add the parent params on each element.