encode / django-rest-framework

Web APIs for Django. 🎸
https://www.django-rest-framework.org
Other
28.41k stars 6.84k forks source link

Object-level validation not working on ModelSerializer #2252

Closed calina-c closed 9 years ago

calina-c commented 9 years ago

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.

tomchristie commented 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.

calina-c commented 9 years ago

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.

tomchristie commented 9 years ago

Great. Thanks for the update!

xingheng commented 5 years ago

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

xordoquy commented 5 years ago

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.

frankdilo commented 3 years ago

I confirm that my custom object-level validate method is not being called with a serializers.ModelSerializer, with no other steps failing before.

Tobeyforce commented 2 years ago

I confirm that my custom object-level validate method is not being called with a serializers.ModelSerializer, with no other steps failing before.

Are you actually calling is_valid on it? It won't run if you don't :)

amithuu commented 10 months ago

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 
shivamforever commented 10 months ago

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

DarkhanShaga commented 5 months ago

I confirm that my custom object-level validate method is not being called with a serializers.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.