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

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

Open
cotyq opened this issue May 30, 2024 · 0 comments
Open

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

cotyq opened this issue May 30, 2024 · 0 comments

Comments

@cotyq
Copy link

cotyq commented May 30, 2024

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.

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

1 participant