django之序列化数据

序列化数据安装命令:pip install djangorestframework在setting设置文件中的INSTALLED_APPS选项中添加‘rest_framework’
INSTALLED_APPS = [
    ...
    ‘rest_framework‘,
]
1、在model同级目录下新建py文件2、新建类->继承serializers.ModelSerializer例如:#序列化类继承
from rest_framework import serializers
from app(你的APP名称).models import *(表名)
class Aserializer(serializers.ModelSerializer):  time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")#净化时间输出格式
class Meta: #表名 model = 表名 fields = "__all__"
3、view视图里调用
from app.serializers import *
transactions = 表名.objects.filter.all()
transactions_ser = Aserializer(transactions, many=True)
# print(transactions)
# print(transactions_ser.data)
data = {
    ‘data‘: transactions_ser.data
}