Python - Django - 序列化
app01/__int__.py:
import pymysql pymysql.install_as_MySQLdb()
app01/models.py:
from django.db import models class Person(models.Model): name = models.CharField(max_length=16) age = models.IntegerField() def __str__(self): return self.name
创建了数据库 Person
添加一下数据
views.py:
from django.shortcuts import render, HttpResponse from app01 import models def person_info(request): info = models.Person.objects.all() from django.core import serializers ret = serializers.serialize("json", info) # 把数据转换成 json 类型 return HttpResponse(ret)
运行结果: