user2307087
user2307087

Reputation: 423

Django REST framework and form validation

I am starting using Django REST framework, I found the framework doesn't have the same level of form validation. If I use form validation, I have the endpoint as the form, and serializes the data, which seems it is not a benefit to use the framework. How can I solve this?

Upvotes: 1

Views: 1571

Answers (1)

jvc26
jvc26

Reputation: 6523

Assuming what you are referring to is object level validation. (I.e. acting on more than one field together) you need to do this in the serializer (as per serializer docs) this is done by overriding:

def validate(self, data):

On the serializer class.

An example of this (from the included link):

def validate(self, data):
    """
    Check that the start is before the stop.
    """
    if data['start'] > data['finish']:
        raise serializers.ValidationError("finish must occur after start")
    return data

Upvotes: 4

Related Questions