Serialzers 序列化組件

Serialzers 序列化組件

前言

  • 當作先後端分離的項目時,JSON是一個輕量級的數據交互格式。全部咱們給前端數據的時候都要轉成json格式,那就須要對咱們從數據庫拿到數據進行序列化

Django的序列化方法

  • 使用django,json轉數據進行傳輸,(瞭解便可)
class BooksView(View):
    def get(self, request):
    	# 獲取數據庫中的queryset數據
        book_list = Book.objects.values("id", "title", "chapter", "pub_time", "publisher")
        # 強轉成列表類型
        book_list = list(book_list)
        # 若是咱們須要取外鍵關聯的字段信息 須要循環獲取外鍵 再去數據庫查而後拼接成咱們想要的
        ret = []
        for book in book_list:
            pub_dict = {}
            pub_obj = Publish.objects.filter(pk=book["publisher"]).first()
            pub_dict["id"] = pub_obj.pk
            pub_dict["title"] = pub_obj.title
            book["publisher"] = pub_dict
            ret.append(book)
        ret = json.dumps(book_list, ensure_ascii=False, cls=MyJson)
        return HttpResponse(ret)


# json.JSONEncoder.default()
# 解決json不能序列化時間字段的問題
class MyJson(json.JSONEncoder):
    def default(self, field):
        if isinstance(field, datetime.datetime):
            return field.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(field, datetime.date):
            return field.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, field)
  • 因爲太過於麻煩,全部咱們可使用DRF的序列化

字段和選項

  • 注意serializer不是隻能爲數據庫模型類定義,也能夠爲非數據庫模型定義。serializer是獨立於數據庫以外的存在

1.字段和選項

<table style="border-spacing: 0px; font-size: 16px; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; letter-spacing: .2px; background-color: #ffffff;"> <thead> <tr style="border-top-color: #cccccc;"><th>字段</th><th>字段構造方式</th></tr> </thead> <tbody> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">BooleanField</span></td> <td>BooleanField()</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">NullBooleanField</span></td> <td>NullBooleanField()</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">CharField</span></td> <td>CharField(max_length=None, min_length=None, allow_blank=False, trim_whitespace=True)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">EmailField</span></td> <td>EmailField(max_length=None, min_length=None, allow_blank=False)</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">RegexField</span></td> <td>RegexField(regex, max_length=None, min_length=None, allow_blank=False)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">SlugField</span></td> <td>SlugField(max<span>length=50, min_length=None, allow_blank=False)&nbsp;<br />正則字段,驗證正則模式 [a-zA-Z0-9</span>-]+</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">URLField</span></td> <td>URLField(max_length=200, min_length=None, allow_blank=False)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">UUIDField</span></td> <td>UUIDField(format='hex_verbose')&nbsp;<br />format:&nbsp;<br />1)&nbsp;<code style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: .85em; border: none; color: inherit; background-color: #f7f7f7;">'hex_verbose'</code>&nbsp;如<code style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: .85em; border: none; color: inherit; background-color: #f7f7f7;">"5ce0e9a5-5ffa-654b-cee0-1238041fb31a"</code>&nbsp;<br />2)&nbsp;<code style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: .85em; border: none; color: inherit; background-color: #f7f7f7;">'hex'</code>&nbsp;如&nbsp;<code style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: .85em; border: none; color: inherit; background-color: #f7f7f7;">"5ce0e9a55ffa654bcee01238041fb31a"</code>&nbsp;<br />3)<code style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: .85em; border: none; color: inherit; background-color: #f7f7f7;">'int'</code>&nbsp;- 如:&nbsp;<code style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: .85em; border: none; color: inherit; background-color: #f7f7f7;">"123456789012312313134124512351145145114"</code>&nbsp;<br />4)<code style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: .85em; border: none; color: inherit; background-color: #f7f7f7;">'urn'</code>&nbsp;如:&nbsp;<code style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: .85em; border: none; color: inherit; background-color: #f7f7f7;">"urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a"</code></td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">IPAddressField</span></td> <td>IPAddressField(protocol='both', unpack_ipv4=False, **options)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">IntegerField</span></td> <td>IntegerField(max_value=None, min_value=None)</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">FloatField</span></td> <td>FloatField(max_value=None, min_value=None)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">DecimalField</span></td> <td>DecimalField(max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None)<br />max_digits: 最多位數<br />decimal_palces: 小數點位置</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">DateTimeField</span></td> <td>DateTimeField(format=api_settings.DATETIME_FORMAT, input_formats=None)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">DateField</span></td> <td>DateField(format=api_settings.DATE_FORMAT, input_formats=None)</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">TimeField</span></td> <td>TimeField(format=api_settings.TIME_FORMAT, input_formats=None)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">DurationField</span></td> <td>DurationField()</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">ChoiceField</span></td> <td>ChoiceField(choices)<br />choices與Django的用法相同</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">MultipleChoiceField</span></td> <td>MultipleChoiceField(choices)</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">FileField</span></td> <td>FileField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">ImageField</span></td> <td>ImageField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL)</td> </tr> <tr style="border-top-color: #cccccc;"> <td><span style="font-weight: bold;">ListField</span></td> <td>ListField(child=, min_length=None, max_length=None)</td> </tr> <tr style="background-color: #f8f8f8; border-top-color: #cccccc;"> <td><span style="font-weight: bold;">DictField</span></td> <td>DictField(child=)</td> </tr> </tbody> </table>html

2.選項參數

<table style="border-spacing:0px;font-size:16px;color:rgb(51,51,51);font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;letter-spacing:.2px;background-color:rgb(255,255,255);"><thead><tr style="border-top-color:rgb(204,204,204);"><th>參數名稱</th><th>做用</th></tr></thead><tbody><tr style="border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">max_length</span></td><td>最大長度</td></tr><tr style="background-color:rgb(248,248,248);border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">min_lenght</span></td><td>最小長度</td></tr><tr style="border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">allow_blank</span></td><td>是否容許爲空</td></tr><tr style="background-color:rgb(248,248,248);border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">trim_whitespace</span></td><td>是否截斷空白字符</td></tr><tr style="border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">max_value</span></td><td>最小值</td></tr><tr style="background-color:rgb(248,248,248);border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">min_value</span></td><td>最大值</td></tr></tbody></table>前端

3.通用參數

<table style="border-spacing:0px;font-size:16px;color:rgb(51,51,51);font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;letter-spacing:.2px;background-color:rgb(255,255,255);"><thead><tr style="border-top-color:rgb(204,204,204);"><th>參數名稱</th><th>說明</th></tr></thead><tbody><tr style="border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">read_only</span></td><td>代表該字段僅用於序列化輸出,默認False</td></tr><tr style="background-color:rgb(248,248,248);border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">write_only</span></td><td>代表該字段僅用於反序列化輸入,默認False</td></tr><tr style="border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">required</span></td><td>代表該字段在反序列化時必須輸入,默認True</td></tr><tr style="background-color:rgb(248,248,248);border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">default</span></td><td>反序列化時使用的默認值</td></tr><tr style="border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">allow_null</span></td><td>代表該字段是否容許傳入None,默認False</td></tr><tr style="background-color:rgb(248,248,248);border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">validators</span></td><td>該字段使用的驗證器</td></tr><tr style="border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">error_messages</span></td><td>包含錯誤編號與錯誤信息的字典</td></tr><tr style="background-color:rgb(248,248,248);border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">label</span></td><td>用於HTML展現API頁面時,顯示的字段名稱</td></tr><tr style="border-top-color:rgb(204,204,204);"><td><span style="font-weight:700;">help_text</span></td><td>用於HTML展現API頁面時,顯示的字段幫助提示信息</td></tr></tbody></table>python

DRF序列化的方法

1. 自定義序列化

  • 須要自定義全部字段,以及方法create,updata方法git

  • 聲明一個序列化類數據庫

    # 根據model.py中的字段對應寫要顯示的字段
    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField()
        title = serializers.CharField(max_length=32)
        CHOICES = ((1, "Linux"), (2, "Django"), (3, "Python"))
        chapter = serializers.ChoiceField(choices=CHOICES, source="get_chapter_display")
        pub_time = serializers.DateField()
  • 將數據庫中的數據傳入序列化對象中django

    from rest_framework.views import APIView
    from rest_framework.response import Response
    
    class BookView(APIView):
        def get(self, request):
            # 獲取數據庫中的數據
            book_list = Book.objects.all()
            # 實例化序列化對象, many= True 表示數據有多個,若是是一個則不須要
            ret = BookSerializer(book_list, many=True)
            # 給前端的數據
            return Response(ret.data)

2. 外鍵關係的序列化

from rest_framework import serializers
from .models import Book


class PublisherSerializer(serializers.Serializer):
    # read_only=True  表示的是給前端時顯示,反序列時,不須要此字段
    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(max_length=32)


class UserSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    name = serializers.CharField(max_length=32)
    age = serializers.IntegerField()


class BookSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(max_length=32)
    CHOICES = ((1, "Linux"), (2, "Django"), (3, "Python"))
    chapter = serializers.ChoiceField(choices=CHOICES, source="get_chapter_display", read_only=True)
    pub_time = serializers.DateField()

    publisher = PublisherSerializer(read_only=True)
    # many= True 表示有多條數據
    user = UserSerializer(many=True, read_only=True)

3. 反序列化

  • 當前端給咱們發post請求的時候,前端給後端傳數據的,咱們須要對數據進行一些校驗而後保存到數據庫,或者對數據庫中的數據進行更改,DRF的serializer也提供了方法json

  • Serializer提供了is_valid()和save方法後端

  • url.pyapi

    urlpatterns = [
        url(r'^list$', BooksView.as_view()),
    ]
  • 聲明一個序列化類app

    # serializers.py 文件
    class BookSerializer(serializers.Serializer):
        # read_only = True  表示前端不須要傳該字段的數據,其餘的都須要傳
        id = serializers.IntegerField(read_only=True)
        title = serializers.CharField(max_length=32)
        CHOICES = ((1, "Linux"), (2, "Django"), (3, "Python"))
        chapter = serializers.ChoiceField(choices=CHOICES, source="get_chapter_display", read_only=True)
        # write_only = True 表示前端須要傳這個字段的數據,  
        w_chapter = serializers.IntegerField(write_only=True)
        pub_time = serializers.DateField()
    
        publisher = PublisherSerializer(read_only=True)
        user = UserSerializer(many=True, read_only=True)
    
        users = serializers.ListField(write_only=True)
        publisher_id = serializers.IntegerField(write_only=True)
    # post請求會執行該方法
        def create(self, validated_data):
            book = Book.objects.create(title=validated_data["title"], chapter=validated_data["w_chapter"], pub_time=validated_data["pub_time"],                                  publisher_id=validated_data["publisher_id"])
            book.user.add(*validated_data["users"])
            # 須要返回對象
            # 若是數據簡單,
            # book = Book.objects.create(**validated_data)
            return book
    • 注意: 由於存在後端給前端顯示的數據和前端給後端存儲的數據有區別,全部要靈活運用read_only= True 和write_only=True ,倆個屬性,
  • 反序列化

    class BookView(APIView):
        def get(self, request):
            book_list = Book.objects.all()
            ret = BookSerializer(book_list, many=True)
            return Response(ret.data)
        # 會執行序列化中的create方法
        def post(self, request):
            # book_obj = request.data
            print(request.data)
            # 將前端傳來的數據傳入序列化對象中
            serializer = BookSerializer(data=request.data)
            # 判斷數據
            if serializer.is_valid():
                # 執行後端的create方法
                serializer.save()
                return Response(serializer.validated_data)
            # 不然將錯誤信息發回給前端
            return Response(serializer.errors)
  • 對數據進行編輯

    url.py

    urlpatterns = [
        url(r'book/(?P<pk>\d+)/', BookView.as_view(), name='book'),
    ]

    聲明一個序列化類

    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField(read_only=True)
        title = serializers.CharField(max_length=32)
        CHOICES = ((1, "Linux"), (2, "Django"), (3, "Python"))
        chapter = serializers.ChoiceField(choices=CHOICES, source="get_chapter_display", read_only=True)
        w_chapter = serializers.IntegerField(write_only=True)
        pub_time = serializers.DateField()
    
        publisher = PublisherSerializer(read_only=True)
        user = UserSerializer(many=True, read_only=True)
    
        users = serializers.ListField(write_only=True)
        publisher_id = serializers.IntegerField(write_only=True)
    
        def create(self, validated_data):
            book = Book.objects.create(title=validated_data["title"], chapter=validated_data["w_chapter"], pub_time=validated_data["pub_time"],
                                       publisher_id=validated_data["publisher_id"])
            book.user.add(*validated_data["users"])
            return book
    # put或者patch或執行該方法
        def update(self, instance, validated_data):
            instance.title = validated_data.get("title", instance.title)
            instance.chapter = validated_data.get("w_chapter", instance.chapter)
            instance.pub_time = validated_data.get("pub_time", instance.pub_time)
            instance.publisher_id = validated_data.get("publisher_id", instance.publisher_id)
            if validated_data.get("users"):
                instance.user.set(validated_data.get("users"))
            instance.save()
            return instance

    view.py

    class BookView(APIView):
         def patch(self, request):
            print(request.data)
            book_id = request.data["id"]
            book_info = request.data["book_info"]
            book_obj = Book.objects.filter(pk=book_id).first()
            # partial= True 對部分數據進行修改
            serializer = BookSerializer(book_obj, data=book_info, partial=True)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data)
            else:
                return Response(serializer.errors)
    
         # 這倆種方法是同樣的只寫其中一種
        def put(self, request, id):
            book_obj = Book.objects.filter(id=id).first()
            ser_obj = BookSerializer(instance=book_obj, data=request.data, partial=True)
            if ser_obj.is_valid():
                ser_obj.save()
                return Response(ser_obj.data)
            return Response(ser_obj.errors)

3.1 view四個方法的運用

-- get 請求
   def get(self, request):
      queryset = Book.objects.all()
      ser_obj = BookSerializer(queryset, many=True)
      return Response(ser_obj.data)


-- post請求
   def post(self, request):
      data = request.data
      ser_obj = BookSerializer(data=request.data)
      if ser_obj.is_valid():
         ser_obj.save()
         # ser_obj.validated_data
         return Response(ser_obj.data)
      else:
         return Response(ser_obj.errors)
        
        
-- put/patch請求
   def put(self, request, id):
      data = request.data
      book_obj = Book.objects.filter(id=id).first()
      ser_obj = BookSerializer(instance=book_obj, data=request.data, partial=True)  # partial表示局部更新
      if ser_obj.is_valid():
         ser_obj.save()
         # ser_obj.validated_data
         return Response(ser_obj.data)
      else:
         return Response(ser_obj.errors)

4.驗證

  • 優先級從高到低: 自定義>單獨驗證>多個字段驗證

  • 若是咱們須要對某些字段進行自定義驗證,DRF也給咱們提供了鉤子方法

  • 一 對單個數據進行驗證, 格式必須是validate_須要驗證的字段名(self, value):

    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField(read_only=True)
        title = serializers.CharField(max_length=32)
        # 省略了一些字段 跟上面代碼裏同樣的
        # 。。。。。
        # 格式必須是validate_須要驗證的字段名(self, value)
         def validate_title(self, value):
            if "python" not in value.lower():
                raise serializers.ValidationError("標題必須含有Python")
            return value
  • 對多個數據進行驗證

    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField(read_only=True)
        title = serializers.CharField(max_length=32)
        CHOICES = ((1, "Linux"), (2, "Django"), (3, "Python"))
        chapter = serializers.ChoiceField(choices=CHOICES, source="get_chapter_display", read_only=True)
        w_chapter = serializers.IntegerField(write_only=True)
        pub_time = serializers.DateField()
        date_added = serializers.DateField(write_only=True)
        # 新增了一個上架時間字段  
        # 省略一些字段。。都是在原基礎代碼上增長的
        # 。。。。。。
    
        # 對多個字段進行驗證 要求上架日期不能早於出版日期 上架日期要大
        def validate(self, attrs):
            if attrs["pub_time"] > attrs["date_added"]:
                raise serializers.ValidationError("上架日期不能早於出版日期")
            return attrs
  • 定義一個驗證器

    def my_validate(value):
        if "敏感詞彙" in value.lower:
            raise serializers.ValidationError("包含敏感詞彙,請從新提交")
        return value
    
    
    class BookSerializer(serializers.Serializer):
        id = serializers.IntegerField(read_only=True)
        title = serializers.CharField(max_length=32, validators=[my_validate])
        # 。。。。。。

ModelSerializer

  • 如今咱們已經清楚了Serializer的用法,會發現咱們全部的序列化跟咱們的模型都緊密相關~

    那麼,DRF也給咱們提供了跟模型緊密相關的序列化器ModelSerializer

      -- 它會根據模型自動生成一組字段

      -- 它簡單的默認實現了.update()以及.create()方法

1.定義一個ModelSerializer序列化器

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"
        # fields = ["id", "title", "pub_time"]
        # exclude = ["user"]
        # 分別是全部字段 包含某些字段 排除某些字段

2.外鍵關聯的序列化

自動序列化連表操做,可使用depth來進行快捷的跨表操做,官方建議是0~10層,可是最好用到3或者4層就能夠了

注意:當序列化類MATE中定義了depth時,這個序列化類中引用字段(外鍵)則自動變爲只讀

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"
        # fields = ["id", "title", "pub_time"]
        # exclude = ["user"]
        # 分別是全部字段 包含某些字段 排除某些字段
        depth = 1
# depth 表明找嵌套關係的第幾層

3.自定義字段對覆蓋默認字段

咱們能夠聲明一些字段來覆蓋默認字段,來進行自定製~

好比咱們的選擇字段,默認顯示的是選擇的key,咱們要給用戶展現的是value。

class BookSerializer(serializers.ModelSerializer):
    chapter = serializers.CharField(source="get_chapter_display", read_only=True)
    
    class Meta:
        model = Book
        fields = "__all__"
        # fields = ["id", "title", "pub_time"]
        # exclude = ["user"]
        # 分別是全部字段 包含某些字段 排除某些字段
        depth = 1

5QL0FN.png

4.meta中的其它關鍵字參數

class BookSerializer(serializers.ModelSerializer):
    chapter = serializers.CharField(source="get_chapter_display", read_only=True)

    class Meta:
        model = Book
        fields = "__all__"
        # fields = ["id", "title", "pub_time"]
        # exclude = ["user"]
        # 分別是全部字段 包含某些字段 排除某些字段
        depth = 1
        # read_only_fields 表示只讀
        read_only_fields = ["id"]
        extra_kwargs = {"title": {"validators": [my_validate,]}}
  • read_only_fields 表示id爲只讀,

  • extra_kwargs 選項在字段上指定任意附加關鍵字參數。與 read_only_fields 的狀況同樣,這意味着你不須要在序列化類中顯式聲明該字段。

  • 該選項是一個字典,將字段名稱映射到關鍵字參數字典。例如:

    class CreateUserSerializer(serializers.ModelSerializer):
        class Meta:
            model = User
            fields = ('email', 'username', 'password')
            extra_kwargs = {'password': {'write_only': True}}

5 post以及patch/put請求

  • 因爲depth會讓咱們外鍵變成只讀,因此咱們再定義一個序列化的類,其實只要去掉depth就能夠了~~
class BookSerializer(serializers.ModelSerializer):
    chapter = serializers.CharField(source="get_chapter_display", read_only=True)

    class Meta:
        model = Book
        fields = "__all__"
        # fields = ["id", "title", "pub_time"]
        # exclude = ["user"]
        # 分別是全部字段 包含某些字段 排除某些字段
        read_only_fields = ["id"]
        extra_kwargs = {"title": {"validators": [my_validate,]}}

6 SerializerMethodField

  • 針對於多對多表數據的顯示,可使用SerializerMethodField,來自定義顯示

外鍵關聯的對象有不少字段咱們是用不到的~都傳給前端會有數據冗餘~就須要咱們本身去定製序列化外鍵對象的哪些字段~~

class BookSerializer(serializers.ModelSerializer):
    chapter = serializers.CharField(source="get_chapter_display", read_only=True)
    user = serializers.SerializerMethodField()
    publisher = serializers.SerializerMethodField()

    def get_user(self, obj):
        # obj是當前序列化的book對象
        users_query_set = obj.user.all()
        return [{"id": user_obj.pk, "name": user_obj.name} for user_obj in users_query_set]

    def get_publisher(self, obj):
        publisher_obj = obj.publisher
        return {"id": publisher_obj.pk, "title": publisher_obj.title}

    class Meta:
        model = Book
        fields = "__all__"
        # fields = ["id", "title", "pub_time"]
        # exclude = ["user"]
        # 分別是全部字段 包含某些字段 排除某些字段
        read_only_fields = ["id"]
        extra_kwargs = {"title": {"validators": [my_validate,]}}

5QLV8H.png

7. 用ModelSerializer改進上面Serializer的完整版

class BookSerializer(serializers.ModelSerializer):
    dis_chapter = serializers.SerializerMethodField(read_only=True)
    users = serializers.SerializerMethodField(read_only=True)
    publishers = serializers.SerializerMethodField(read_only=True)

    def get_users(self, obj):
        # obj是當前序列化的book對象
        users_query_set = obj.user.all()
        return [{"id": user_obj.pk, "name": user_obj.name} for user_obj in users_query_set]

    def get_publishers(self, obj):
        publisher_obj = obj.publisher
        return {"id": publisher_obj.pk, "title": publisher_obj.title}

    def get_dis_chapter(self, obj):
        return obj.get_chapter_display()

    class Meta:
        model = Book
        # fields = "__all__"
        # 字段是有序的
        fields = ["id", "title","dis_chapter", "pub_time", "publishers", "users","chapter", "user", "publisher"]
        # exclude = ["user"]
        # 分別是全部字段 包含某些字段 排除某些字段
        read_only_fields = ["id", "dis_chapter", "users", "publishers"]
        extra_kwargs = {"title": {"validators": [my_validate,]}, "user": {"write_only": True}, "publisher": {"write_only": True},
                        "chapter": {"write_only": True}}

返回URL的鏈接

詳情見網址:https://www.cnblogs.com/wupeiqi/articles/7805382.html中的序列化中的url

5QLPGl.png

相關文章
相關標籤/搜索