Django實(shí)現(xiàn)從數(shù)據(jù)庫(kù)中獲取到的數(shù)據(jù)轉(zhuǎn)換為dict
這種方式只能應(yīng)用于從數(shù)據(jù)庫(kù)中獲取到的單條數(shù)據(jù),例如models.Users.objects.get()獲取到的數(shù)據(jù)
from django.forms.models import model_to_dictclass Index(VIew): def get(self, request): userObj = models.Users.objects.get(id = 1) userDict = model_to_dict(userObj) print(userDict) return HttpResponse(’yes’)
重點(diǎn)是導(dǎo)入的model_to_dict方法
補(bǔ)充知識(shí):django自定義標(biāo)簽使用,Bytes/KB/MB/GB相互轉(zhuǎn)換
目錄結(jié)構(gòu)
templatetags--mytags.pyviews.py
后端代碼 mytags.py
from django import templateregister = template.Library()#bytes單位轉(zhuǎn)換@register.simple_tag()def bytes_convert(num): if not num: return ’’ elif num < 1024: return str(num) + ’ B’ elif 1024 <= num < 1024*1024: return str(round(num/1024,2)) + ’ KB’ elif 1024*1024 <= num < 1024*1024*1024: return str(round(num/(1024*1024),2)) + ’ MB’ else: return str(round(num/(1024*1024*1024),2)) + ’ GB’
前端代碼
{% load mytags %} <--??胱遠(yuǎn)?x?嘶`--><td>{% bytes_convert i.bytes %}</td> <--使用?嘶`-->
以上這篇Django實(shí)現(xiàn)從數(shù)據(jù)庫(kù)中獲取到的數(shù)據(jù)轉(zhuǎn)換為dict就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. AJAX的跨域問(wèn)題解決方案2. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. 爬取今日頭條Ajax請(qǐng)求4. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令5. phpstudy apache開(kāi)啟ssi使用詳解6. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器7. jsp文件下載功能實(shí)現(xiàn)代碼8. 詳解瀏覽器的緩存機(jī)制9. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程10. uniapp解決軟鍵盤(pán)彈出問(wèn)題方法詳解
