django讀取資料庫(以json格式輸出)

2021-10-10 16:17:03 字數 2406 閱讀 1506

2. 無限極迴圈

request為get模式時:

def

getmultidata

(request)

:if request.method ==

"get"

: title = request.get.get(

'title'

)// 獲取引數

needdata = multidata.objects.

filter

(title=title)

// 根據引數獲取資料庫資料

json_data = serializers.serialize(

'json'

, needdata)

// 將資料轉為json格式

json_data = json.loads(json_data)

// 將資料轉為json格式

data =

for i in

range

(len

(json_data)):

['fields'])

// 提取想要的資料到陣列中

finaldata =

return jsonresponse(finaldata, safe=

false

)// 輸出到前端

request為post模式時:

def

getmultidata

(request)

:if request.method ==

"post"

: title = request.post.get(

'title'

)// 獲取引數

needdata = multidata.objects.

filter

(title=title)

// 根據引數獲取資料庫資料

json_data = serializers.serialize(

'json'

, needdata)

// 將資料轉為json格式

json_data = json.loads(json_data)

// 將資料轉為json格式

data =

for i in

range

(len

(json_data)):

['fields'])

// 提取想要的資料到陣列中

finaldata =

return jsonresponse(finaldata, safe=

false

)// 輸出到前端

def

category_data

(request)

: category = categorydata.objects.

all(

)// 資料庫資料

category_info = serializers.serialize(

'json'

, category)

// 將資料轉為json格式

category_info = json.loads(category_info)

category_info_data =

for i in

range

(len

(category_info)):

['fields'])

lists =

tree =

pid =

''for i in category_info_data:

item = i

tree[item[

'cat_id']]

= item

root =

none

for i in category_info_data:

obj = i

ifnot obj[

'pid']:

root = tree[obj[

'cat_id']]

else

: pid = obj[

'pid']if

'childlist'

notin tree[pid]

: tree[pid]

['childlist']=

tree[pid]

['childlist'

]'cat_id']]

) data =

,'code'

:200

,'msg'

:'true'

}return jsonresponse(data, safe=

false

)

django查詢資料庫

特殊過濾 django資料庫查詢時,一般就是 modelname.objects.a ll modelname.objects.filter modelname.objects.get 等等,在過濾時,如果過濾條件假設是其中的某一欄位,比如 id,要求id 5,或者是乙個範圍比如1 id 5,前者可...

django資料庫查詢

特殊過濾 django資料庫查詢時,一般就是 modelname.objects.a ll modelname.objects.filter modelname.objects.get 等等,在過濾時,如果過濾條件假設是其中的某一欄位,比如 id,要求id 5,或者是乙個範圍比如1 id 5,前者可...

Django資料庫設定

今天下午跟著教程學習怎樣連線資料庫,我使用的是mysql,遇到了不少問題,在此記錄下來 環境 django version 1.6.3,python version 2.7 1.settings.py檔案設定template dirs 路徑中一定不要用反斜槓,正確設定方法 template dirs...