Django rest_framework时间区间筛选

项目场景:

基于用户体验,查看在某一时间段内的内容 


代码演示:

以某团任意时间段内的订单筛选查询为例

表模型设计:

class Info(models.Model):
    name=models.CharField(verbose_name='订单名称')
    publish_time = models.DateTimeField(null=True, blank=True, verbose_name='完成时间 ')

    class Meta:
    db_table = 'tb_info'
    verbose_name = '订单详情'

view视图(序列化器略)

class InfoView(generics.ListCreateAPIView):
    queryset = models.Infor.objects.all()
    serializer_class = serlalizer.InfomationSerializer
    #分页
    pagination_class = 
    filter_backends = [filters.OrderingFilter]

    ordering_fields = ['stats', 'publish_time']
    ordering = ['stats', 'publish_time']

    
    def get_queryset(self):
        start_time=self.request.GET.get('start_time')
        end_time=self.request.GET.get('end_time')
        if start_time and end_time:
            queryset=models.Informations.objects.filter(publish_time__range=(start_time,end_time))
            return queryset
        queryset = self.queryset.all()
        return queryset

#欢迎投稿!!!!


你可能感兴趣的:(大数据)