from django.db import models # Create your models here. class Book(models.Model): nid = models.AutoField(primary_key=True) name = models.CharField(max_length=32) price = models.DecimalField(max_digits=5, decimal_places=2) publish_date = models.DateField(null=True) xx = models.IntegerField(choices=((0, '文學類'), (1, '情感類')), default=1, null=True) publish = models.ForeignKey(to='Publish', to_field='nid', on_delete=models.CASCADE, null=True) authors = models.ManyToManyField(to='Author') def __str__(self): return self.name def xxx(self): return '123xxx' class Author(models.Model): nid = models.AutoField(primary_key=True) name = models.CharField(max_length=32) age = models.IntegerField() def __str__(self): return self.name class Publish(models.Model): nid = models.AutoField(primary_key=True) name = models.CharField(max_length=32) city = models.CharField(max_length=32) email = models.EmailField() def __str__(self): return self.name def xx2(self): return 'abc123'
# app01serializer.py from rest_framework import serializers from app01 import models class BookSerializer(serializers.Serializer): name = serializers.CharField() # 指定source=‘name’ ,表示序列化模型表中的name字段,重命名爲name1 # name 和 source=‘name’ 指定的name不能重名 # name1 = serializers.CharField(source='name') # write_only 序列化的時候,該字段不顯示 # read_only 反序列化的時候,該字段不傳 price = serializers.CharField() publish = serializers.CharField(source='publish.name') # source不但能夠制定一個字段,也能夠指定一個方法 # 下面的內容就是指定的方法 func_con = serializers.CharField(source='xxx') # 跨表指定方法 func_con2 = serializers.CharField(source='publish.xx2') book_type = serializers.CharField(source='get_xx_display', read_only=True) # 序列化出版社的詳情,指定SerializerMethodField以後,能夠對應一個方法, # 返回什麼內容,publish_detail就是什麼內容 publish_detail = serializers.SerializerMethodField(read_only=True) # 對應方法固定寫法: get_字段名字 def get_publish_detail(self, obj): print(type(obj)) return {'name': obj.publish.name, 'city': obj.publish.city} # 返回全部做者信息 authors = serializers.SerializerMethodField(read_only=True) def get_authors(self, obj): authorser = AuthorSerializer(obj.authors.all(), many=True) return authorser.data
# views.py部分 from app01 import models from app01.app01serializer import BookSerializer from rest_framework.views import APIView from rest_framework.response import Response class Book(APIView): def get(self, request, *args, **kwargs): response = {'code': 100, 'msg': '查詢成功!'} books = models.Book.objects.all() booker = BookSerializer(instance=books, many=True) print(type(booker.data)) response['data'] = booker.data return Response(response) def post(self, request): # 實例化產生一個序列化類的對象,data是要反序列化的字典 booker = BookSerializer(data=request.data) if booker.is_valid(raise_exception=True): # 校驗booker數據格式 booker.create(booker.validated_data) return Response()
# app01serializer.py from rest_framework import serializers from app01 import models class BookSerializer(serializers.ModelSerializer): class Meta: model = models.Book fields = '__all__' depth = 1 # 跨表 (與下方被註釋部分目的同樣) # exclude=('name') # 與field不能同時用 """ xx = serializers.CharField(source='get_xx_display') authors = serializers.SerializerMethodField() def get_authors(self, obj): ret = AuthorSerializer(instance=obj.authors.all(), many=True) return ret.data """
# views.py部分 def post(self,request): #實例化產生一個序列化類的對象,data是要反序列化的字典 bookser=BookSerializer(data=request.data) # bookser.data if bookser.is_valid(raise_exception=True): #清洗經過的數據 bookser.save() else: print(bookser.errors['name'][0]) return Response()