【django】序列化
1.对象
对于Django的Queryset<Queryset [object,object,object]>这种关于django的Querysetset对象这种是不能用python的json方法,需要用到django的 serializers模块
传递给 serialize 方法的参数有二点:
a.个序列化目标格式,
b.一个是序列号的对象QuerySet. (事实上,第二个参数可以是任何可迭代的Django Model实例,但它很多情况下就是一个QuerySet).
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.conf.urls import url from django.contrib import admin from app01 import views from app02 import views as v2 from app03 import views as v3 urlpatterns = [ url(r'^xuliehua/', v3.xuliehua), url(r'^get_data/', v3.get_data), ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>用户列表</h1> <table id="tb"> </table> <script src="/static/jquery-3.1.1.js/"></script> <script> $(function () { initData() }); //页面执行完毕,自动执行函数 function initData() { $.ajax({ url:'/get_data/', type:'GET', dataType:'JSON', success:function (arg) { //$('#tb').append(arg); if (arg.status){ //console.log(arg.data); var v = JSON.parse(arg.data); console.log(v); } } }) } </script> </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% for row in user_list %} <tr> <td>{{ row.id }}</td> <td>{{ row.username }}</td> <td>{{ row.email }}</td> </tr> {% endfor %} </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.shortcuts import render,HttpResponse from app01 import models import json def xuliehua(request): return render(request,'xuliehua.html') def get_data(request): from django.core import serializers ret ={'status':True,'data':None} try: # 1.对象转字符串(序列化) user_list = models.UserInfo.objects.all() # QuerySet【obj,obj,obj】 ret['data'] = serializers.serialize("json",user_list) # 通过Django 内置序列化 except Exception as e: ret['status'] = False result = json.dumps(ret) return HttpResponse(result)
2.字典
关于python一些内置一些类型(例如:字典,列表,元祖。。。)的序列化,就用json 就可以了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户列表</h1>
<table id="tb">
</table>
<script src="/static/jquery-3.1.1.js/"></script>
<script>
$(function () {
initData()
});
//页面执行完毕,自动执行函数
function initData() {
$.ajax({
url:'/get_data/',
type:'GET',
dataType:'JSON',
success:function (arg) {
//$('#tb').append(arg);
if (arg.status){
console.log(arg.data);
//var v = JSON.parse(arg.data);
//console.log(v);
}
}
})
}
</script>
</body>
</html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.shortcuts import render,HttpResponse from app01 import models import json def xuliehua(request): return render(request,'xuliehua.html') def get_data(request): from django.core import serializers ret ={'status':True,'data':None} try: # 2.字典转字符串(序列化) user_list = models.UserInfo.objects.all().values('id','username') ret['data'] = list(user_list) # user_list最外面querySet,里面是字典;list(user_list)相当于把querySet list化 except Exception as e: ret['status'] = False result = json.dumps(ret) return HttpResponse(result)
3.元组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>用户列表</h1> <table id="tb"> </table> <script src="/static/jquery-3.1.1.js/"></script> <script> $(function () { initData() }); //页面执行完毕,自动执行函数 function initData() { $.ajax({ url:'/get_data/', type:'GET', dataType:'JSON', success:function (arg) { //$('#tb').append(arg); if (arg.status){ console.log(arg.data); //var v = JSON.parse(arg.data); //console.log(v); } } }) } </script> </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.shortcuts import render,HttpResponse from app01 import models import json def xuliehua(request): return render(request,'xuliehua.html') def get_data(request): from django.core import serializers ret ={'status':True,'data':None} try: # 3.元组转字符串(序列化) user_list = models.UserInfo.objects.all().values_list('id', 'username') ret['data'] = list(user_list) # user_list最外面querySet,里面是元组;list(user_list)相当于把querySet list化 except Exception as e: ret['status'] = False result = json.dumps(ret) return HttpResponse(result)