Closed calina-c closed 9 years ago
Could you give a simple code example of what you're trying to do here, exactly? Both of those options should work fine, but not obvious if this is something due to a bug, or due to poor documentation, or due to a misunderstanding.
Sorry, closing this since it was a misunderstanding. Non-field errors appear only if per-field validators pass. I wasn't aware of that previously.
Great. Thanks for the update!
Reopen this.
I found the object validation not working with custom validate(self, data)
in the serializer class, after digging into the is_valid
method inside, I found the object validation method should be run_validation(self, data)
, this works for me.
So, why does the documentation not update for this? Or am I wrong?
Django 2.2 with DRF 3.10
It's not called likely because some previous steps have failed before it can call validate
.
As far as I can tell, documentation is correct as it is.
If you think there's an issue with the documentation we'll require more specific informations about the issue.
I confirm that my custom object-level validate
method is not being called with a serializers.ModelSerializer
, with no other steps failing before.
I confirm that my custom object-level
validate
method is not being called with aserializers.ModelSerializer
, with no other steps failing before.
Are you actually calling is_valid on it? It won't run if you don't :)
def run_validation(self, data): if data['max_years_experience'] < data['min_years_experience']: raise serializers.ValidationError({'message': 'max_years_experience cannot be less than min_years_experience'}) else: return super().validate(data)
run_validation() works
thanks @xingheng
Sorry, closing this since it was a misunderstanding. Non-field errors appear only if per-field validators pass. I wasn't aware of that previously.
Hey, but you said previously that its happening with object level validation
I confirm that my custom object-level
validate
method is not being called with aserializers.ModelSerializer
, with no other steps failing before.
It is probably because you call it without parenthesis, and you probably have init in it. You need to call it like that ExampleValidator() and there should be no init in it, only call.
I'm having an object with a min and a max field, which I need to validate on a class level. However, several methods don't work. I tried:
Field level validators work. The issue appears both at editing with PUT and creating with POST.