Im trying to simply perform a post request that will save a new competition to the database.
# views.py
class CompetitionCreateView(generics.CreateAPIView):
serializer_class = CompetitionSerializer
queryset = Competition.objects.all()
def create(self, request, *args, **kwargs):
serializer = CompetitionSerializer(data=request.data)
if serializer.is_valid():
competition = serializer.save()
return Response(
data=CompetitionSerializer(competition).data,
status=status.HTTP_201_CREATED,
content_type="json")
return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST, content_type="json")
# serializer.py
class CompetitionSerializer(serializers.ModelSerializer):
class Meta:
model = Competition
fields = "__all__"
response:
I have tried using regular APIView and switching from def create(...)
to def post(...)
but no luck. Also tried to parse the request data content to json.
This is the traceback from the console
Internal Server Error: /competition-create
test_deploy_web | Traceback (most recent call last):
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
test_deploy_web | response = get_response(request)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
test_deploy_web | response = wrapped_callback(request, *callback_args, **callback_kwargs)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
test_deploy_web | return view_func(*args, **kwargs)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
test_deploy_web | return self.dispatch(request, *args, **kwargs)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
test_deploy_web | response = self.handle_exception(exc)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
test_deploy_web | self.raise_uncaught_exception(exc)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
test_deploy_web | raise exc
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
test_deploy_web | response = handler(request, *args, **kwargs)
test_deploy_web | File "/app/view/views.py", line 21, in post
test_deploy_web | if serializer.is_valid():
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 220, in is_valid
test_deploy_web | self._validated_data = self.run_validation(self.initial_data)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 419, in run_validation
test_deploy_web | value = self.to_internal_value(data)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py", line 476, in to_internal_value
test_deploy_web | validated_value = field.run_validation(primitive_value)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py", line 568, in run_validation
test_deploy_web | value = self.to_internal_value(data)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py", line 1899, in to_internal_value
test_deploy_web | return self.model_field.to_python(data)
test_deploy_web | File "/usr/local/lib/python3.9/site-packages/django/contrib/postgres/fields/ranges.py", line 80, in to_python
test_deploy_web | vals = json.loads(value)
test_deploy_web | File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads
test_deploy_web | return _default_decoder.decode(s)
test_deploy_web | File "/usr/local/lib/python3.9/json/decoder.py", line 340, in decode
test_deploy_web | raise JSONDecodeError("Extra data", s, end)
test_deploy_web | json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)
It was a problem with input JSON that was not being parsed correctly.