好友
阅读权限10
听众
最后登录1970-1-1
|
使用django_filters 库,分别过滤DateField 字段和DateTimeField 字段,返回的结果差异问题
- 需求是想基于
时间字段 ,实现按年 ,按月 ,按日 过滤数据
- 问题点: 如果时间类型是
DateField ,过滤完全没有问题;如果时间类型是DateTimeField ,只能按年 过滤,按月 和按日 过滤的结果都是空,搞不懂为什么
- DRF 代码如下
### models
from django.db import models
class BaseModel(models.Model):
"""基类时间模型,继承专用"""
create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
class Meta:
abstract = True
class Departments(BaseModel):
"""
组织架构 部门
"""
name = models.CharField(max_length=32, unique=True, verbose_name='部门')
pid = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父部门')
demo_date = models.DateField(null=True, blank=True, verbose_name='演示日期')
### serilizers
from .models import Departments
from rest_framework import serializers
class DepartmentsModelSerializer(serializers.ModelSerializer):
class Meta:
model = Departments
fields = '__all__'
### filters
from .models import Departments
from django_filters import rest_framework as filters
class DepartmentsFilter(filters.FilterSet):
# demo_date (DateField)过滤器
year = filters.NumberFilter(field_name='demo_date', lookup_expr='year', label='Year')
month = filters.NumberFilter(field_name='demo_date', lookup_expr='month', label='Month')
day = filters.NumberFilter(field_name='demo_date', lookup_expr='day', label='Day')
# create_time (DateTimeField) 的过滤器
create_year = filters.NumberFilter(field_name='create_time', lookup_expr='year', label='Create Year')
create_month = filters.NumberFilter(field_name='create_time', lookup_expr='month', label='Create Month')
create_day = filters.NumberFilter(field_name='create_time', lookup_expr='day', label='Create Day')
class Meta:
model = Departments
fields = ['year', 'month', 'day', 'create_year', 'create_month', 'create_day']
### views
from .models import Departments
from .filters import DepartmentsFilter
from django_filters import rest_framework as filters
from rest_framework.viewsets import ModelViewSet
class DepartmentsModelViewSet(ModelViewSet):
queryset = Departments.objects.all()
ordering_fields = ('id', 'name')
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = DepartmentsFilter
- 然后往
MySQL 随便插入一点测试数据
- 前端传参,测试效果
demo_date 字段按年 ,按月 ,按日 ,测试结果如下(正常返回过滤结果,没有问题)
- http://localhost:8000/api/departments/?year=2024
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"label": "业务部",
"pid": null,
"create_time": "2023-05-18 09:14:40",
"update_time": "2024-12-18T09:14:40.926475Z",
"name": "业务部",
"demo_date": "2024-12-18"
},
{
"id": 3,
"label": "人事部",
"pid": null,
"create_time": "2024-12-18 09:18:47",
"update_time": "2024-11-18T09:18:47Z",
"name": "人事部",
"demo_date": "2024-11-18"
}
]
}
- http://localhost:8000/api/departments/?month=12
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"label": "业务部",
"pid": null,
"create_time": "2023-05-18 09:14:40",
"update_time": "2024-12-18T09:14:40.926475Z",
"name": "业务部",
"demo_date": "2024-12-18"
}
]
}
- http://localhost:8000/api/departments/?year=2023&day=9
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 2,
"label": "企划部",
"pid": null,
"create_time": "2024-12-18 09:18:40",
"update_time": "2024-12-18T09:18:40.693082Z",
"name": "企划部",
"demo_date": "2023-02-09"
}
]
}
create_time 字段按年 ,按月 ,按日 ,测试结果如下(只有年份正常返回数据,月和日都是空,数据库的数据无误)
- http://localhost:8000/api/departments/?create_year=2024
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"id": 2,
"label": "企划部",
"pid": null,
"create_time": "2024-12-18 09:18:40",
"update_time": "2024-12-18T09:18:40.693082Z",
"name": "企划部",
"demo_date": "2023-02-09"
},
{
"id": 3,
"label": "人事部",
"pid": null,
"create_time": "2024-12-18 09:18:47",
"update_time": "2024-11-18T09:18:47Z",
"name": "人事部",
"demo_date": "2024-11-18"
}
]
}
- http://localhost:8000/api/departments/?create_month=12
{
"count": 0,
"next": null,
"previous": null,
"results": []
}
- http://localhost:8000/api/departments/?create_day=18
{
"count": 0,
"next": null,
"previous": null,
"results": []
}
- 求助大佬,为什么有这种现象?
月 和日 的查询结果都是空?如果想正常返回需求结果,该如何修改代码?
- 基于
DateTimeField 实现按年 ,按月 和按日 过滤数据,有什么更好的办法吗?求大佬 指导,非常感谢!!!
|
|