程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Mekol Studio - django+harmonyos uses ROM method to query the latest data

編輯:Python

Django+HarmonyOS Use ROM Method to query the latest data

Preface

Use Django If you need to operate the existing database , Model mapping is required first

The specific operation is as follows

# CMD operation , Enter the project file
python manage.py inspectdb
# Wait for the mapping to complete
python manage.py inspectdb > models.py
# After the manage.py Generate new models.py file , Just copy and paste

One 、 Back end

Similarly, we need to start with urls.py Use in as_view() Method to configure the route , The specific reasons have been explained in the previous chapter

# Return the latest data in the database 
class Getlastdata(APIView):
def post(self, request):
try:
patientname = request.data.get('patientname') # Get the front-end transmission data 
print(' The user enters a name :' + patientname)
result = models.Patient.objects.filter(name=patientname).last() # Query the latest data that meets the conditions 
name = result.name
age = result.age
height = result.height
weight = result.weight
body = result.body
alldata = []
alldata.append({

'name': name, 'age': age, 'height': height, 'weight': weight, 'body': body
})
alldata_json = json.dumps(alldata, ensure_ascii=False) # Convert to JSON Format 
return HttpResponse(alldata_json)
except Patient.DoesNotExist as e:
print(' The username does not exist ')
return HttpResponse(' The username does not exist ')
else:
return HttpResponse(' request was aborted ')

Two 、 front end JS

# get.js
import fetch from '@system.fetch';
import qs from 'querystring';
import router from '@system.router';
export default{

data:
{

winfo:'',
detail:[{
 // Write initial value 
name:'aaa',
age :'18',
height:'180',
weight:'70',
body:' Leg '
}]
},
onClick(){

fetch.fetch({

url:`http://127.0.0.1:8000/Qingtian/getlastdata/`,
data:qs.stringify({
patientname:'bbb'}), // Request to transmit back-end data 
responseType:'json',
method:'POST',
success:(resp)=>
{

var getdata
getdata = JSON.parse(resp.data) // Back end data front end assignment 
this.detail[0].name = getdata[0].name
this.detail[0].age = getdata[0].age
this.detail[0].height = getdata[0].height
this.detail[0].weight = getdata[0].weight
this.detail[0].body = getdata[0].body
this.winfo = resp.data
console.log(' Return the data :'+this.winfo),
console.log(' data format :'+typeof(this.winfo))
},
fail:(resp)=>
{

console.log(' Failed to get data ')
}
})
}
}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved