[PYTHON] How to write custom validations in the Django REST Framework

Overview

I checked it when I made the app, so I will write it so that I can call it when I forget it.

In Django, I used to put a clean method in the form for validation, but I thought that Django REST Framework would write custom validation in the first place, so I investigated it.

How to write

Write each validation in seliarizers.py.

Function based

Write as follows.

#Validate for adults
def validate_age(age):
    if age < 21:
      raise serializers.ValidationError('It's too early for minors!')

Class based

In class base, write using the __call__ method.

In addition, since it is highly reusable on a class basis, if you basically use this, the visibility of the code will be better.

class AgeValidator(object):
    def __init__(self,num):
        self.num = num
    def __call__(self,age):
        if age < self.num:
            message = 'This page is for Adult!'
            raise serializers.ValidationError(message)

Recommended Posts

How to write custom validations in the Django REST Framework
How to create a Rest Api in Django
How to deal with garbled characters in json of Django REST Framework
How to get people to try out django rest framework features in one file
How to write soberly in pandas
How to reset password via API using Django rest framework
How to generate a query using the IN operator in Django
How to reflect CSS in Django
How to write Django1.9 environment-independent wsgi.py
In Django, how to abbreviate the long displayed string as ....
The 15th offline real-time how to write reference problem in Python
The 14th offline real-time how to write reference problem in python
Create a REST API to operate dynamodb with the Django REST Framework
The 18th offline real-time how to write reference problem in Python
Logical deletion in Django, DRF (Django REST Framework)
Understand the benefits of the Django Rest Framework
How to check the version of Django
How to delete expired sessions in Django
Miscellaneous notes about the Django REST framework
Various comments to write in the program
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
How to write this process in Perl?
How to write Ruby to_s in Python
Implementation of custom user model authentication in Django REST Framework with djoser
The 17th offline real-time how to write reference problem implemented in Python
How to write the correct shebang in Perl, Python and Ruby scripts
How to build an application from the cloud using the Django web framework
How to use the C library in Python
How to implement Rails helper-like functionality in Django
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
How to write regular expression patterns in Linux
How to print debug messages to the Django console
The trick to write flatten concisely in python
Django REST framework A little useful to know.
How to get the files in the [Python] folder
Implement JWT login functionality in Django REST framework
How to write async and await in Vue.js
How to write a named tuple document in 2020
Implementing authentication in Django REST Framework with djoser
How to uniquely identify the source of access in the Django Generic Class View
How to count the number of elements in Django and output to a template
How to automatically generate API document with Django REST framework & POST from document screen
I'm addicted to the difference in how Flask and Django receive JSON data
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
Django REST framework basics
Django Rest Framework Tips
20th Offline Real-time How to Write Problems in Python
How to retrieve the nth largest value in Python
Learning notes for the migrations feature in the Django framework (2)
How to get the variable name itself in python
How to run the Ansible module added in Ansible Tower
How to write a GUI using the maya command
How to get multiple model objects randomly in Django
How to get the number of digits in Python
How to write string concatenation in multiple lines in Python
How to know the current directory in Python in Blender
Create a Todo app with the Django REST framework
Set the form DateField to type = date in Django