序列化程序中的布尔字段 – Django REST 框架
在 Django REST Framework 中,序列化的概念是将 DB 数据转换为 javascript 可以使用的数据类型。每个序列化程序都带有一些将要处理的字段(条目)。例如,如果您有一个名为 Employee 的类,其字段为 Employee_id、Employee_name、is_admin 等。那么,您将需要 AutoField、CharField 和 BooleanField 来通过 Django 存储和操作数据。同样,序列化器也以相同的原理工作,并具有用于创建序列化器的字段。
本文围绕 Django REST 框架中的序列化程序中的布尔字段展开。布尔值有两个主要字段——BooleanField 和 NullBooleanField。
布尔字段
用于包装True或False值的布尔字段。它的工作原理与 BooleanField – Django 模型相同。默认情况下,创建的 serializers.BooleanField 实例默认为 False。
句法 -
field_name = serializers.BooleanField()
Null布尔字段
接受True 、 False和Null值的布尔字段。它的工作原理与 NullBooleanField – Django 模型相同。默认情况下,serializers.NullBooleanField 实例默认创建为无。
句法 -
field_name = serializers.NullBooleanField()
如何在序列化程序中使用布尔字段?
为了解释布尔字段的用法,让我们使用相同的项目设置 – 如何使用 Django Rest 框架创建基本 API?
现在您的项目中有一个名为 serializers 的文件,让我们创建一个以 BooleanField 和 NullBooleanField 作为字段的序列化程序。
Python3
#import serializer from rest_framework
from rest_framework import serializers
class Geeks(object):
def __init__(self, bool1, bool2):
self.field_1 = bool1
self.field_2 = bool2
# create a serializer
class GeeksSerializer(serializers.Serializer):
# initialize fields
field_1 = serializers.BooleanField()
field_2 = serializers.NullBooleanField()
现在让我们创建一些对象并尝试序列化它们并检查它们是否真的在工作,运行,-
Python manage.py shell
现在,在 shell 中运行以下Python命令
# 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}
这是终端上所有这些操作的输出 -
序列化器字段中的核心参数
.math-table { 边框折叠:折叠;宽度:100%; } .math-table td { 边框:1px 实心 #5fb962;文本对齐:左!重要;填充:8px; } .math-table th { 边框:1px 实心 #5fb962;填充:8px; } .math-table tr>th{ 背景颜色:#c6ebd9;垂直对齐:中间; } .math-table tr:nth-child(odd) { background-color: #ffffff; }Argument Description read_only Set this to True to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization write_only Set this to True to ensure that the field may be used when updating or creating an instance, but is not included when serializing the representation. required Setting this to False also allows the object attribute or dictionary key to be omitted from output when serializing the instance. default If set, this gives the default value that will be used for the field if no input value is supplied. allow_null Normally an error will be raised if None is passed to a serializer field. Set this keyword argument to True if None should be considered a valid value. source The name of the attribute that will be used to populate the field. validators A list of validator functions which should be applied to the incoming field input, and which either raise a validation error or simply return. error_messages A dictionary of error codes to error messages. label A short text string that may be used as the name of the field in HTML form fields or other descriptive elements. help_text A text string that may be used as a description of the field in HTML form fields or other descriptive elements. initial A value that should be used for pre-populating the value of HTML form fields.