I have a method in a ViewSet
where I want to validate that request.data
is a list. If it is not, I would like to raise a ParseError()
.
However, my code crashes when I actually raise said ParseError
and I don't understand why. It is a subclass of APIException
, which the documentation states to be handled automatically.
# views.py
class MyViewSet(viewsets.GenericViewSet):
...
@action(methods=['post'], detail=False, url_path='bulk-create')
def bulk_create(self, request, *args, **kwargs):
# Assert that request.data is a list of objects
if not isinstance(request.data, list):
raise ParseError("Expected the data to be in list format.")
# Actually process incoming data
...
Calling the route with anything other than a list correctly triggers my if
statement, but instead of returning a proper response with status.HTTP_400_BAD_REQUEST
, the server crashes and I receive the following error:
Traceback (most recent call last):
2022-05-17T12:23:45.366216553Z File "/opt/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
2022-05-17T12:23:45.366221178Z response = get_response(request)
2022-05-17T12:23:45.366223345Z File "/opt/venv/lib/python3.9/site-packages/debug_toolbar/middleware.py", line 67, in __call__
2022-05-17T12:23:45.366225470Z panel.generate_stats(request, response)
2022-05-17T12:23:45.366238220Z File "/opt/venv/lib/python3.9/site-packages/debug_toolbar/panels/request.py", line 30, in generate_stats
2022-05-17T12:23:45.366240470Z "post": get_sorted_request_variable(request.POST),
2022-05-17T12:23:45.366346803Z File "/opt/venv/lib/python3.9/site-packages/debug_toolbar/utils.py", line 227, in get_sorted_request_variable
2022-05-17T12:23:45.366352511Z return [(k, variable.getlist(k)) for k in sorted(variable)]
2022-05-17T12:23:45.366354803Z TypeError: '<' not supported between instances of 'dict' and 'dict'
Because of the error log, I suspect that the django-debug-toolbar
got something to do with my problem. I have already tried moving it to every position in my middleware config, but that did not solve my problem.
# settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_currentuser.middleware.ThreadLocalUserMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'rollbar.contrib.django.middleware.RollbarNotifierMiddleware',
]
EDIT: I understand that a sort fails at some point, but why is that sort happening in the first place?
EDIT 2: The people over at django-debug-toolbar
helped me confirm, that it is the JSON formatted as array that clashes with the library when raising an exception.
Thanks to the awesome people at django-debug-toolbar
, the library can now handle top-level arrays in POST.data
as well. https://github.com/jazzband/django-debug-toolbar/pull/1624
Sadly, I ran directly into the same issue with Django's testing system...
# test_with_error.py
from rest_framework.test import APITestCase
class DonationBulkCreateRouteTestCase(APITestCase):
def test_route_permissions(self):
self.client.post(
path="/bulk-create/",
data=json.dumps([], default=json_serializer)
)
# log.txt
File "/opt/venv/lib/python3.9/site-packages/django/test/client.py", line 245, in encode_multipart
for (key, value) in data.items():
AttributeError: 'str' object has no attribute 'items'
So, although top-level arrays are valid JSON, I decided to wrap my data in an additional object, in which I pass the list. This has the additional benefit of being easier to extend in the future, because I can freely add keys without breaking existing code.