From 07b61207050161ef068805fb8ed57190e0304bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Wrona?= Date: Thu, 14 Nov 2024 23:07:35 +0100 Subject: [PATCH] Updated docs to support Django REST Framework. --- docs/usage.rst | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/usage.rst b/docs/usage.rst index 397e309..b1e2063 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -152,3 +152,31 @@ Example usage ajax refresh $('#id_captcha_0').val(result['key']) }); }); + +Example usage in Django REST Framework +--------------------------------- + +To use CAPTCHA in a serializer, simply inherit ``CaptchaSerializer``:: + + from captcha.serializers import CaptchaSerializer + from rest_framework import serializers + + class CheckCaptchaSerializer(CaptchaSerializer): + email = serializers.EmailField() + +…or use ``CaptchaModelSerializer``:: + + from captcha.serializers import CaptchaModelSerializer + from rest_framework import serializers + + class CheckCaptchaModelSerializer(CaptchaModelSerializer): + sender = serializers.EmailField() + + class Meta: + model = User + fields = ("captcha_code", "captcha_hashkey", "sender") + +``CaptchaSerializer`` and ``CaptchaModelSerializer`` implement the ``captcha_code`` and ``captcha_hashkey`` fields. +In case of invalid captcha or hash code an exception is raised:: + + raise exceptions.ValidationError({"error": gettext_lazy("Invalid CAPTCHA")}) \ No newline at end of file