Serializer.is_valid () doesn't work although `required = False` - Django REST Framework
I have a serializer like this:
class DataSetColumnSerializer(serializers.ModelSerializer):
custom_target = serializers.PrimaryKeyRelatedField(required=False)
class Meta:
model = dataset_models.DataSetColumn
Nevertheless:
ipdb> columns[0]
{u'display_name': u'guid', u'name': u'guid', u'data_type': u'STRING', u'custom_target': None, u'ignore': False, u'is_identifier': False, u'order': 1}
ipdb> serializer.is_valid()
False
ipdb> serializer.errors
[{'custom_target': [u'This field cannot be blank.']}, {'custom_target': [u'This field cannot be blank.']}, {'custom_target': [u'This field cannot be blank.']}, {'custom_target': [u'This field cannot be blank.']}, {'custom_target': [u'This field cannot be blank.']}, {'custom_target': [u'This field cannot be blank.']}, {'custom_target': [u'This field cannot be blank.']}, {'custom_target': [u'This field cannot be blank.']}]
ipdb> serializer.fields['custom_target'].required
False
What's happening?
+3
source to share
1 answer
From the docs:
Note. When validation is applied to the ModelSerializer, both the serializer fields and the corresponding model fields must be validated correctly. If you have additional fields on your model, make sure to set blank = True correctly on the model field and also set required = False on the serializer field.
+8
source to share