Django--ModelSerializer

 

定义ModelSerializer

# 序列器初体验
class BookInfoSerializer(serializers.ModelSerializer):
    """图书管理序列化器"""

    class Meta:
        # model 是指该序列化器的处理的数据字段是从模型类的BookInfo中生成的
        model = BookInfo
        # fields指该序列化器包含模型类的那些字段,‘__all__包含所有’ 可以列举写  exclude是除此之外
        fields = '__all__'
        # exclude = ['image']

具体实现结果:

>>> from booktest.serializers import BookInfoSerializer
>>> s = BookInfoSerializer()
>>> s
BookInfoSerializer():
    id = IntegerField(label='ID', read_only=True)
    btitle = CharField(label='名称', max_length=20)
    bpub_date = DateField(label='发布日期')
    bread = IntegerField(label='阅读量', max_value=2147483647, min_value=-2147483648, required=False)
    bcomment = IntegerField(label='评论量', max_value=2147483647, min_value=-2147483648, required=False)
    is_delete = BooleanField(label='逻辑删除', required=False)
    image = ImageField(allow_null=True, label='图片', max_length=100, required=False)

field可以指明具体字段

# fields = (__all__)    #__all__包含所有字段
fields = ('id', 'btitle', 'bpub_date')   #指明具体字段
exclude = ('image',)  #指明不包含的字段

 HeroInfoSerializer 序列化器中添加外键关联

hbook = BookInfoSerializer()

或者:

class BookInfoSerializer(serializers.ModelSerializer):
    """英雄人物管理序列化器"""

    class Meta:
        model = HeroInfo
        fields = '__all__'
        depth=1
depth = 1  #默认的ModelSerialzer是主键作为关联字段,使用depth实现简单的嵌套 depth是整数,表名嵌套的层级数量

具体的实现结果是:
>>> from booktest.serializers import HeroInfoSerializer
>>> HeroInfoSerializer()
HeroInfoSerializer():
    id = IntegerField(label='ID', read_only=True)
    hname = CharField(label='英雄名字', max_length=20)
    hgender = ChoiceField(choices=((0, 'male'), (1, 'female')), label='性别', required=False)
    hcomment = CharField(allow_null=True, label='描述信息', max_length=200, required=False)
    hbook = BookInfoSerializer():
        id = IntegerField(label='ID', read_only=True)
        btitle = CharField(label='名称', max_length=20)
        bpub_date = DateField(label='发布日期')
        bread = IntegerField(label='阅读量', max_value=2147483647, min_value=-2147483648, required=False)
        bcomment = IntegerField(label='评论量', max_value=2147483647, min_value=-2147483648, required=False)
        is_delete = BooleanField(label='逻辑删除', required=False)
        image = ImageField(allow_null=True, label='图片', max_length=100, required=False)

 指明只读字段

read_only_field = ('id','bread','bcomment')  # 指明只读字段,进用于序列化输出的字段 可以使列表或元组 默认是双向的

添加其他参数:

extra_kwargs = {
            'bread':{'min_value':0,'required':True},
            'bcomment':{'min_value':0,'required':True}
        }

extra_kwargs:追加额外字段的约束,修改原有的选项参数,优先级高于模型类自己的映射,只有优先级高才能替换之前的数据

>>> from booktest.serializers import BookInfoSerializer
>>> BookInfoSerializer()
BookInfoSerializer():
    id = IntegerField(label='ID', read_only=True)
    btitle = CharField(label='名称', max_length=20)
    bpub_date = DateField(label='发布日期')
    bread = IntegerField(label='阅读量', max_value=2147483647, min_value=0, required=True)
    bcomment = IntegerField(label='评论量', max_value=2147483647, min_value=0, required=True)
    is_delete = BooleanField(label='逻辑删除', required=False)
    image = ImageField(allow_null=True, label='图片', max_length=100, required=False)

 

 

 

你可能感兴趣的:(Django)