I'm trying to validate empty fields in an endpoint by overwriting to_internal_value
function and displaying an error message with ValidationError, very similar as the following answer:
serializer.py
def to_internal_value(self, data):
missing = []
for k in ['comments']:
try:
if data[k] == '':
missing.append(k)
except Exception as e:
missing.append(k)
if len(missing):
raise serializers.ValidationError("The following fields are required: %s" % ','.join(missing))
return data
The problem is that I get: Error: too many values to unpack (expected 2)
when raise serializers.ValidationError
instruction is executed and data
is comming with comments field empty (''):
(Pdb) data
<QueryDict: {'csrfmiddlewaretoken': ['<csrfmiddlewaretoken>'], 'comments': [''], 'user': ['']}>
Even testing with a simple string:
raise serializers.ValidationError("The following fields are required: comments")
I receive the same error. In the python console, raise throw the error:
>>> from rest_framework.serializers import ValidationError
>>> from rest_framework import serializers
>>> data={'comments': [''], 'user': ['']}
>>> missing=[]
>>> missing.append('comments')
>>> raise serializers.ValidationError("The following fields are required: %s" % ','.join(missing))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
rest_framework.exceptions.ValidationError: [ErrorDetail(string='The following fields are required: comments', code='invalid')]
Instead of serializers.ValidationError()
I had to use ValueError()
and It works good, but I guess that it's not the suggested way.
Edit I was using:
Django==2.2.4
djangorestframework==3.10.3
Then I upgraded to:
Django==2.2.9
djangorestframework==3.11.0
With the same results.