vue前端和Django后端如何查詢一定時(shí)間段內(nèi)的數(shù)據(jù)
前言
在開發(fā)過程中經(jīng)常會(huì)遇到篩選查詢之類的功能,比如查詢某一個(gè)時(shí)間段內(nèi)的數(shù)據(jù)而非所有數(shù)據(jù)。
這樣我們就需要向后端發(fā)送時(shí)間段的參數(shù),然后在后端處理查詢。
這里以Django后端和vue前端的簡(jiǎn)單例子來記錄大致實(shí)現(xiàn)。
后端數(shù)據(jù)庫
這里是一些簡(jiǎn)單的數(shù)據(jù)重要的是date,我們需要根據(jù)日期來篩選返回到前端。

models.py
class CountDownSign(models.Model): name = models.CharField(max_length=1000) date = models.DateField() sign = models.CharField(max_length=200)
serializers.py
這里引入的是drf框架,但篩選查詢的思路和這個(gè)框架沒有關(guān)系。
class CountDownModelSerializer(serializers.ModelSerializer):
class Meta:
model = CountDownSign
fields = '__all__'
def create(self, validated_data):
return CountDownSign.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.name = validated_data.get('name', instance.name)
instance.date = validated_data.get('date', instance.date)
instance.sign = validated_data.get('sign', instance.sign)
instance.save()
return instance
views.py
為篩選查詢提供接口。拿到前端傳遞的起止日期。核心代碼如下
obj = models.CountDownSign.objects.filter(date__range=(start, end))
class CountDownViewSet(ModelViewSet):
parser_classes = [JSONParser, FormParser]
"""視圖集"""
queryset = models.CountDownSign.objects.all()
serializer_class = CountDownModelSerializer
# 搜索
search_fields = ('id', 'name', 'sign', 'date')
@action(methods=['post'], detail=False)
def getSE(self, request, *args, **kwargs):
start = request.data.get('start', None)
end = request.data.get('end', None)
if start and end:
obj = models.CountDownSign.objects.filter(date__range=(start, end))
if obj:
ser = CountDownModelSerializer(instance=obj, many=True)
print(ser.data)
return JsonResponse({
'code': '200',
'msg': '獲取數(shù)據(jù)成功',
'data': ser.data
})
else:
return JsonResponse({
'code': '1002',
'msg': '獲取失敗',
})
else:
return Response(status=status.HTTP_204_NO_CONTENT)
前端界面
這里簡(jiǎn)略給出用于接收起止時(shí)間的兩個(gè)date-picker,并且給搜索綁定事件。
<div class="datePicker"> <div class="block" style="float: left"> <el-date-picker v-model="value1" type="datetime" value-format="yyyy-MM-dd" placeholder="請(qǐng)選擇選擇開始日期"> </el-date-picker> </div> <div class="block" style="float: left; margin-left: 20px;"> <el-date-picker v-model="value2" type="datetime" value-format="yyyy-MM-dd" placeholder="請(qǐng)選擇截止日期"> </el-date-picker> </div> <el-button round style="float: left; margin-left: 20px;" @click="searchC">搜索</el-button> </div>
data.js
實(shí)現(xiàn)的接口函數(shù)
export function searchCountDown(start, end) {
return request({
url: 'countDown/getSE/',
method: 'post',
data: {
start: start,
end: end
}
})
}
點(diǎn)擊事件的實(shí)現(xiàn)
判斷輸入的合法性,并接受數(shù)據(jù)進(jìn)行數(shù)據(jù)綁定展示
searchC() {
console.log(this.value1);
console.log(this.value2);
if (this.value1 < this.value2) {
searchCountDown(this.value1, this.value2).then(res => {
console.log(res.data);
this.searchRes = res.data;
})
} else {
this.$message.error("時(shí)間范圍出錯(cuò)");
}
},
數(shù)據(jù)展示
<div class="article">
<ul>
<li v-for="(item,index) in searchRes">
<div class="ui grid" style="width: 100%;height: 60px;">
<div class="four wide column"><span>{{ item.name }}</span></div>
<div class="four wide column"><span>{{ item.date }}</span></div>
<div class="four wide column"><span>{{ item.sign }}</span></div>
<div class="four wide column">
<el-button type="danger" icon="el-icon-delete" circle @click="deleteC(item.id)"></el-button>
<el-button type="primary" icon="el-icon-edit" circle></el-button>
</div>
</div>
<div class="ui divider"></div>
</li>
</ul>
運(yùn)行結(jié)果
可以看到返回的數(shù)據(jù)均是在時(shí)間范圍內(nèi),這里的2月25號(hào)零時(shí)其實(shí)返回的數(shù)據(jù)是2月5號(hào),因?yàn)檫M(jìn)行了數(shù)據(jù)格式化,所以25號(hào)的數(shù)據(jù)也被返回了。

總結(jié)
到此這篇關(guān)于vue前端和Django后端如何查詢一定時(shí)間段內(nèi)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue和Django查詢數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js移動(dòng)端app之上拉加載以及下拉刷新實(shí)戰(zhàn)
這篇文章主要介紹了vue.js移動(dòng)端app之上拉加載以及下拉刷新實(shí)戰(zhàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
利用vue-router實(shí)現(xiàn)二級(jí)菜單內(nèi)容轉(zhuǎn)換
這篇文章主要介紹了如何利用vue-router實(shí)現(xiàn)二級(jí)菜單內(nèi)容轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
淺談validator自定義驗(yàn)證及易錯(cuò)點(diǎn)
這篇文章主要介紹了validator自定義驗(yàn)證及易錯(cuò)點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
el-date-picker設(shè)置日期默認(rèn)值兩種方法(當(dāng)月月初至月末)
這篇文章主要給大家介紹了關(guān)于el-date-picker設(shè)置日期默認(rèn)值(當(dāng)月月初至月末)的相關(guān)資料,文中通過代碼示例將解決的辦法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
vue 解決mintui彈窗彈起來,底部頁面滾動(dòng)bug問題
這篇文章主要介紹了vue 解決mintui彈窗彈起來,底部頁面滾動(dòng)bug問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11

