• 序列化


    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总结之json解析(FastJson Gson 对比)
    Android性能优化之UncaughtExceptionHandler定制自己的错误日志系统
    IOS遍历网页获取网页中<img>标签中的图片url
    IOS各种集合遍历效率对比
    cx_oracle访问处理oracle中文乱码问题
    使用tar解压文件提示gzip: stdin: not in gzip format错误
    Mac安装crfpp
    oracle 常用操作
    docker启动centos7后sudo不能使用
    常见Python爬虫工具总结
  • 原文地址:https://www.cnblogs.com/heris/p/11905462.html
Copyright © 2020-2023  润新知