溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在Django后端中查詢一定時(shí)間段內(nèi)的數(shù)據(jù)

發(fā)布時(shí)間:2021-03-01 16:49:24 來源:億速云 閱讀:877 作者:Leah 欄目:開發(fā)技術(shù)

如何在Django后端中查詢一定時(shí)間段內(nèi)的數(shù)據(jù)?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

后端數(shù)據(jù)庫

這里是一些簡單的數(shù)據(jù)重要的是date,我們需要根據(jù)日期來篩選返回到前端。

如何在Django后端中查詢一定時(shí)間段內(nèi)的數(shù)據(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)

前端界面

這里簡略給出用于接收起止時(shí)間的兩個(gè)date-picker,并且給搜索綁定事件。

 <div class="datePicker">
 <div class="block" >
 <el-date-picker
  v-model="value1"
  type="datetime"
  value-format="yyyy-MM-dd"
  placeholder="請(qǐng)選擇選擇開始日期">
 </el-date-picker>
 </div>
 <div class="block" >
 <el-date-picker
  v-model="value2"
  type="datetime"
  value-format="yyyy-MM-dd"
  placeholder="請(qǐng)選擇截止日期">
 </el-date-picker>
 </div>
 <el-button round  @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" >
  <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ù)也被返回了。

如何在Django后端中查詢一定時(shí)間段內(nèi)的數(shù)據(jù)

看完上述內(nèi)容,你們掌握如何在Django后端中查詢一定時(shí)間段內(nèi)的數(shù)據(jù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI