I am validating data from a request in django's rest framework with data validation in the serializer. I need all error messages to be sent in a single response.
On sending a string that is too long, i get the error:
django.db.utils.DataError: value too long for type character varying(3)
In my model I've defined the max length of the character field as 3 (max theoretically needed).
I've added validation in the serializer to catch requests with too many characters:
validators.py
class CustomUserValidators():
errors_to_return = {}
def val_role(self, role):
if len(role) > 3:
self.errors_to_return["role_length"] = "Ensure this field has no more than 3 characters."
serializers.py
from Sea.validators import CustomUserValidators
class LagoonUserCreateSerializer(UserCreateSerializer, CustomUserValidators):
class Meta:
model = User
fields = ('id', 'username', 'role',)
def validate(self, attrs):
self.val_role(attrs['role'])
if len(self.errors_to_return) > 0:
raise serializers.ValidationError(self.errors_to_return)
return attrs
models.py
class SeaUser(AbstractUser):
...
role = models.CharField(_('Role'), max_length=3)
But the request still returns the error (value too long... as above). I expected the erorr to be caught in the serializer and the values not passed to the model, why is the value ever reaching the model?
I have researched this, and all the solutions say make the field length 255. This doesn't answer why the value is ever being tested by the model in the first place, and doesn't explain what happend if somehow a longer than expected value does end up in the request.
Any help with this is appreciated.