Hi everyone, I'm trying to customize the a field validation + it's validation error message, as I have the following code:
VIEW: class MyClassViewSet(viewsets.ModelViewSet): serializer_class = MySerializer queryset = mymodel.objects.all() def perform_create(self, serializer): serializer.save() @method_decorator(csrf_protect) def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) SERIALIZER: class MySerializer(serializers.ModelSerializer): class Meta: model = mymodel fields = ['id', 'name', 'email', 'note', 'isdisabled'] def __init__(self, *args, **kwargs): super(MySerializer, self).__init__(*args, **kwargs) validators = self.fields['email'].validators for validator in validators: if isinstance(validator, EmailValidator): validator.message = _("msgInvalidEmail") MODEL: class mymodel(models.Model): name = models.CharField(max_length=100, null=False, default=None, db_column='name') email = models.EmailField(max_length=254, unique=False, blank=True, null=True, db_column='email') note = models.CharField(max_length=500, null=True, blank=True, default=None, db_column='note') licensekey = models.CharField(max_length=100, null=True, db_column='licensekey') isdisabled = models.BooleanField(null=False, default=False, db_column='isdisabled') Problem 1: If I run the code like this, I get a generic error message like this, "This field may not be blank.", when I don't enter name for example. I don't know which field the message is talking about. If I add a validation into my serializer as follow: def validate_name(self, value): if not value: raise serializers.ValidationError(_("msgInvalidName")) def validate_licensekey(self, value): if not value: raise serializers.ValidationError(_("msgInvalidLicenseKey")) The validation method don't get triggered. Only if change my model adding "blank=True" for these fields in my model. Problem 2: If I add the field validation in my serializer and change the mode adding "blank=True" I start getting some weird behaviour when running my unit tests. Problem 3: How can I check if the serializer error message already have an error message and add the "Carriage return + Line feed" to push the next validation error message to the next line? Is there a better way to customize those validation error messages without changing the model attributes? What would a good practice in this case? Any help would be greatly appreciated. Thank you! Regards, Alex -- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/81ac72a9-25bb-47de-ac8b-5970d6c00595%40googlegroups.com.