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ǔ)充知識(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ù)庫中獲取到的數(shù)據(jù)轉(zhuǎn)換為dict就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析2. 利用單元測(cè)試對(duì)PHP代碼進(jìn)行檢查3. python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML4. python excel和yaml文件的讀取封裝5. python3實(shí)現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)6. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決7. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊8. python操作數(shù)據(jù)庫獲取結(jié)果之fetchone和fetchall的區(qū)別說明9. Django 權(quán)限管理(permissions)與用戶組(group)詳解10. App啟動(dòng)優(yōu)化-Android性能優(yōu)化
