Django實(shí)現(xiàn)從數(shù)據(jù)庫中獲取到的數(shù)據(jù)轉(zhuǎn)換為dict
這種方式只能應(yīng)用于從數(shù)據(jù)庫中獲取到的單條數(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ǔ)充知識: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ù)庫中獲取到的數(shù)據(jù)轉(zhuǎn)換為dict就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python使用ProjectQ生成量子算法指令集2. ThinkPHP6使用JWT+中間件實(shí)現(xiàn)Token驗(yàn)證實(shí)例詳解3. JSP出現(xiàn)中文亂碼問題解決方法詳解4. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考5. Python如何解決secure_filename對中文不支持問題6. Python 用NumPy創(chuàng)建二維數(shù)組的案例7. ASP基礎(chǔ)入門第二篇(ASP基礎(chǔ)知識)8. python 定義函數(shù) 返回值只取其中一個(gè)的實(shí)現(xiàn)9. Python函數(shù)參數(shù)中的*與**運(yùn)算符10. 不使用XMLHttpRequest對象實(shí)現(xiàn)Ajax效果的方法小結(jié)
