zoukankan      html  css  js  c++  java
  • 序列化

    from .models import Book
      

    class PublisherSerializer(serializers.Serializer):
      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,'Python'),(3,'Go'))
      category= serializers.ChoiceField(choices=CHOICES,
      source='get_category_display',read_only=True)
      pub_time = serializers.DateField()

      publisher = PublisherSerializer(read_only=True)
      user = UserSerializer(many=True,read_only=True)

      

    id = serializers.IntegerField(read_only=True)
      title = serializers.CharField(max_length=32)
      CHOICES = ((1,'Linux'),(2,'Python'),(3,'Go'))
      category = serializers.ChoiceField(choices=CHOICES,
        source='get_category_display',read_only=True)
      post_category = 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_date):
        book = Book.objects.create(title=validated_data['title'],
        category=validated_data['post_category'], pub_time=validate_data['pub_time'],
        publisher_id=validated_data['publisher_id'])
        book.user.add(*validated_data['users'])
        return book

    写入自己的博客中才能记得长久
  • 相关阅读:
    Android Matrix(坐标矩阵)
    Android 修改底部导航栏navigationbar的颜色
    Java跨平台原理
    Java的平台无关性
    AudioManager --- generateAudioSessionId
    Android中使用logwrapper来重定向应用程序的标准输出
    Linux指令
    HLS -- m3u8档案格式解析
    新購電腦筆記
    Android多媒体--MediaCodec 中文API文档
  • 原文地址:https://www.cnblogs.com/heris/p/11905462.html
Copyright © 2011-2022 走看看