Boolean Fields in Serializers – Django REST Framework

14 Dec, 2021
In Django REST Framework the very concept of Serializing is to convert DB data to a datatype that can be used by javascript. Every serializer comes with some fields (entries) which are going to be processed. For example if you have a class with name Employee and its fields as Employee_id, Employee_name, is_admin, etc. Then, you would need AutoField, CharField and BooleanField for storing and manipulating data through Django. Similarly, serializer also works with same principle and has fields that are used to create a serializer. This article revolves around Boolean Fields in Serializers in Django REST Framework. There are two major fields for Boolean values – BooleanField and NullBooleanField.  
A boolean field used to wrap True or False values. It works the same as BooleanField – Django Models. By default, serializers.BooleanField instances as created by default as False. Syntax – 
field_name = serializers.BooleanField() 
A boolean field that accepts True, False and Null values. It works the same as NullBooleanField – Django Models. By default, serializers.NullBooleanField instances as created by default as none. Syntax –  
field_name = serializers.NullBooleanField()
To explain the usage of Boolean Fields, let’s use the same project setup from – How to Create a basic API using Django Rest Framework ?. Now that you have a file called serializers in your project, let’s create a serializer with BooleanField and NullBooleanField as the fields. 
Python3
#import serializer from rest_frameworkfrom rest_framework import serializers class Geeks(object):    def __init__(self, bool1, bool2):        self.field_1 = bool1        self.field_2 = bool2 # create a serializerclass GeeksSerializer(serializers.Serializer):    # initialize fields    field_1 = serializers.BooleanField()    field_2 = serializers.NullBooleanField()
Now let us create some objects and try serializing them and check if they are actually working, Run, –  
Python manage.py shell
Now, run following python commands in the shell 
# import everything from serializers
>>> from apis.serializers import *

# create a object of type Geeks
>>> obj = Geeks(bool1 = True, bool2 = True)

# serialize the object
>>> serializer = GeeksSerializer(obj)

# print serialized data
>>> serializer.data
{'field_1': True, 'field_2': True}

# another example
>>> obj1 = Geeks(bool1 = True, bool2 = None)
>>> serializer = GeeksSerializer(obj1)
>>> serializer.data
{'field_1': True, 'field_2': None}
Here is the output of all these operations on terminal – 
.math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-child(odd) { background-color: #ffffff; }  
 
rajeev0719singh
sweetyty
Django-REST
rest-framework
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list