如果我们想要使⽤序列化器对应的是Django的模型类,DRF为我们提供了ModelSerializer模型类序列化器来帮助我们快速创建⼀个Serializer类。
ModelSerializer与常规的Serializer相同,但提供了:
基于模型类⾃动⽣成⼀系列字段
基于模型类⾃动为Serializer⽣成validators,⽐如unique_together 包含默认的create()和update()的实现
一、定义了ModelSerializer类
1、__all__
from rest_framework import serializers
from app2.models import *
class NewsChannelSerializer(serializers.ModelSerialzer):
class Meta:
model=NewsChannel
fields='__all__'
测试结果
>>> from app2.serializers1 import *
>>> serializer=NewsChannelSerializer()
>>> serializer
NewsChannelSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(label='频道名称', max_length=30, validators=[<UniqueValidator(queryset=NewsChannel.objects.all())>])
url = CharField(allow_blank=True, allow_null=True, label='频道页面链接', max_length=50, required=False)
2、指定字段
1、model:指明参照那个模型类
2、fields指定模型类中哪些字段需要自动生成序列化器字段
- 如果指定为
__all__
,那么模型类中所有的字段需要自动转化为序列化器字段 - 如果将特定的字段转化为序列化器字段,用元组表示
fields=(‘id’,‘name’,‘leader’,‘interfaces_set’,‘token’)
- exclude指定模型类中哪些字段不需要设置为序列化器字段,其他的字段需要转化
exclude=(‘create_time’,‘update_time’)
3、显示指明字段
from rest_framework import serializers
from app2.models import *
class NewsChannelSerializer(serializers.ModelSerializer):
class Meta:
model=NewsChannel
fields=['id','name']
测试结果
>>> from app2.serializers1 import *
>>> from app2.models import *
>>> serializer=NewsChannelSerializer()
>>> serializer
NewsChannelSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(label='频道名称', max_length=30, validators=[<UniqueValidator(queryset=NewsChannel.objects.all())>])
4、exclude
from rest_framework import serializers
from app2.models import *
class NewsChannelSerializer(serializers.ModelSerializer):
class Meta:
model=NewsChannel
exclude=['id']
测试结果
>>> from app2.serializers1 import *
>>> serializer=NewsChannelSerializer()
>>> serializer
NewsChannelSerializer():
name = CharField(label='频道名称', max_length=30, validators=[<UniqueValidator(queryset=NewsChannel.objects.all())>])
url = CharField(allow_blank=True, allow_null=True, label='频道页面链接', max_length=50, required=False)
5、指明只读字段
可以通过read_only_fields指明只读字段,即仅⽤于序列化输出的字段
from rest_framework import serializers
from app2.models import *
class NewsChannelSerializer(serializers.ModelSerializer):
class Meta:
model=NewsChannel
fields='__all__'
read_only_fields=['id','url']
测试结果:
>>> from app2.serializers1 import *
>>> serializer=NewsChannelSerializer()
>>> serializer
NewsChannelSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(label='频道名称', max_length=30, validators=[<UniqueValidator(queryset=NewsChannel.objects.all())>])
url = CharField(allow_null=True, label='频道页面链接', read_only=True)
6、添加额外参数
我们可以使⽤extra_kwargs参数为ModelSerializer添加或修改原有的选项参数
from rest_framework import serializers
from app2.models import *
class NewsChannelSerializer(serializers.ModelSerializer):
class Meta:
model=NewsChannel
fields='__all__'
extra_kwargs={
'url':{'max_length':60,'required':True}
}
测试结果:
>>> from app2.serializers1 import *
>>> serializer=NewsChannelSerializer()
>>> serializer
NewsChannelSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(label='频道名称', max_length=30, validators=[<UniqueValidator(queryset=NewsChannel.objects.all())>])
url = CharField(allow_blank=True, allow_null=True, label='频道页面链接', max_length=60, required=True)