濮阳杆衣贸易有限公司

主頁 > 知識庫 > Django 查詢數(shù)據(jù)庫返回JSON的實現(xiàn)

Django 查詢數(shù)據(jù)庫返回JSON的實現(xiàn)

熱門標(biāo)簽:成都呼叫中心外呼系統(tǒng)哪家強 地圖標(biāo)注與注銷 西青語音電銷機器人哪家好 宿州電話機器人哪家好 南昌地圖標(biāo)注 無錫智能外呼系統(tǒng)好用嗎 電梯新時達(dá)系統(tǒng)外呼顯示e 百應(yīng)電話機器人總部 旅游廁所地圖標(biāo)注怎么弄

和前端交互全部使用JSON,如何將數(shù)據(jù)庫查詢結(jié)果轉(zhuǎn)換成JSON格式

返回多條數(shù)據(jù)

示例

import json
from django.http import HttpResponse
from django.core import serializers
def db_to_json(request):
    scripts = Scripts.objects.all()[0:1]
    json_data = serializers.serialize('json', scripts)
    return HttpResponse(json_data, content_type="application/json")

返回結(jié)果

[{
 "fields": {
  "script_content": "abc",
  "script_type": "1"
 },
 "model": "home_application.scripts",
 "pk": "03a0a7cf-567a-11e9-8566-9828a60543bb"
}]

功能實現(xiàn)了,但是我需要返回一個約定好的JSON格式,查詢結(jié)果放在 data 中

 {"message": 'success', "code": '0', "data": []}

代碼如下:

import json
from django.http import HttpResponse
from django.core import serializers
def db_to_json2(request):
    # 和前端約定的返回格式
    result = {"message": 'success', "code": '0', "data": []}
    scripts = Scripts.objects.all()[0:1]
    # 序列化為 Python 對象
    result["data"] = serializers.serialize('python', scripts)
    # 轉(zhuǎn)換為 JSON 字符串并返回
    return HttpResponse(json.dumps(result), content_type="application/json")

調(diào)用結(jié)果

{
 "message": "success",
 "code": "0",
 "data": [{
  "fields": {
   "script_content": "abc",
   "script_type": "1"
  },
  "model": "home_application.scripts",
  "pk": "03a0a7cf-567a-11e9-8566-9828a60543bb"
 }]
}

有點難受的是,每條數(shù)據(jù)對象包含 fields,model,pk三個對象,分別代表字段、模型、主鍵,我更想要一個只包含所有字段的字典對象。雖然也可以處理,但還是省點性能,交給前端解析吧。

返回單個對象

代碼:

from django.forms.models import model_to_dict
from django.http import HttpResponse
import json
def obj_json(request):
    pk = request.GET.get('script_id')
 
    script = Scripts.objects.get(pk=pk)
    # 轉(zhuǎn)為字典類型
    script = model_to_dict(script) 
    return HttpResponse(json.dumps(script), content_type="application/json")

返回JSON:

{
 "script_id": "1534d8f0-59ad-11e9-a310-9828a60543bb",
 "script_content": "3",
 "script_name": "3",
 "script_type": "1"
}

到此這篇關(guān)于Django 查詢數(shù)據(jù)庫返回JSON的實現(xiàn)的文章就介紹到這了,更多相關(guān)Django 返回JSON內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Django2.1.7 查詢數(shù)據(jù)返回json格式的實現(xiàn)
  • Django+RestFramework API接口及接口文檔并返回json數(shù)據(jù)操作
  • Django中使用Json返回數(shù)據(jù)的實現(xiàn)方法
  • django實現(xiàn)HttpResponse返回json數(shù)據(jù)為中文
  • Django 返回json數(shù)據(jù)的實現(xiàn)示例
  • Django返回json數(shù)據(jù)用法示例

標(biāo)簽:辛集 七臺河 雅安 西安 渭南 贛州 濰坊 許昌

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Django 查詢數(shù)據(jù)庫返回JSON的實現(xiàn)》,本文關(guān)鍵詞  Django,查詢,數(shù)據(jù)庫,返回,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Django 查詢數(shù)據(jù)庫返回JSON的實現(xiàn)》相關(guān)的同類信息!
  • 本頁收集關(guān)于Django 查詢數(shù)據(jù)庫返回JSON的實現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    黄龙县| 五常市| 乡宁县| 武汉市| 万盛区| 长武县| 拉萨市| 旺苍县| 陇南市| 南投县| 思茅市| 浏阳市| 晋州市| 保亭| 连山| 浦城县| 黎城县| 蕲春县| 康保县| 庆安县| 自贡市| 静安区| 绩溪县| 伊通| 浏阳市| 兰西县| 洛扎县| 潍坊市| 湄潭县| 岳普湖县| 韶关市| 浮梁县| 宣城市| 光泽县| 牟定县| 阿勒泰市| 陈巴尔虎旗| 邵阳市| 兰州市| 余姚市| 霍邱县|