TP_API2.0/app/serializers.py

36 lines
1.1 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from rest_framework import serializers
from django_filters.rest_framework import FilterSet
import django_filters
from app.models import TP
class SerialMyModel(serializers.ModelSerializer):
class Meta:
model = TP
fields = "__all__"
class SerialFilter(FilterSet):
"""
过滤器,支持模糊查询
record_time日期时间格式如 2023-01-01 00:00:00
police_id支持模糊匹配
event_type支持模糊匹配
"""
record_time = django_filters.DateTimeFilter(field_name='record_time', lookup_expr='icontains')
police_id = django_filters.CharFilter(field_name='police_id', lookup_expr='icontains')
event_type = django_filters.CharFilter(field_name='event_type', lookup_expr='icontains')
# 记录时间范围查询
start_time = django_filters.DateTimeFilter(field_name='record_time', lookup_expr='gte')
end_time = django_filters.DateTimeFilter(field_name='record_time', lookup_expr='lte')
class Meta:
# 指定模型
models = TP
# 指定需要模糊查询的字段
fields = ("record_time", "police_id", "event_type",)