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

    写入自己的博客中才能记得长久
  • 相关阅读:
    Unbutu之web环境部署——常用软件安装
    利用百度uaredirect.js判断手机终端并自动跳转
    原生Ajax附件上传简单实例
    shader glsl 函数图举例
    pixijs释放纹理的方法
    pixijs shader透明度设置方法
    pixijs 用canvas的方法
    threejs 解决模型缩小有黑边的解决方案
    threejs 透明模型遮挡后面模型解决方案
    javascript canvas 清除图片空白多余的方法
  • 原文地址:https://www.cnblogs.com/heris/p/11905462.html
Copyright © 2011-2022 走看看