1

Is it possible to validate request method (POST, PUT, GET...) together with queryset in example below?

    def validate_title(self, value):
      qs = Place.objects.filter(title__iexact=value)
      if qs.exists():
        raise serializers.ValidationError("Duplicated title")

      return value

1 Answer 1

5

You could access the request method by using the serializer context as below,

def validate_title(self, value):
    request_method = self.context['request'].method # change is here
    qs = Place.objects.filter(title__iexact=value)
    if qs.exists():
        raise serializers.ValidationError("Duplicated title")

    return value
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.