當(dāng)前循環(huán). | 作用 |
---|---|
default | 數(shù)據(jù)為空時(shí)設(shè)置默認(rèn)值 |
length | 取變量長(zhǎng)度 |
filesizeformat | 文件大小轉(zhuǎn)成可讀 |
slice | 從指定位置到指定位切片 |
date | datetime取到的時(shí)間,轉(zhuǎn)成指定格式 |
safe | 防止XSS攻擊、加上safe才能傳標(biāo)簽 |
truncatechars | 取摘顯示一段剩下的… |
例子
{#格式 值|函數(shù)#} {# 如果沒有值,那么使用默認(rèn)值#} p>{{ bucunzai|default:'空的哦' }}/p> {# 取出變量長(zhǎng)度#} q>{{ name }}--{{ name|length }}/q> {# 文件大小轉(zhuǎn)換成可讀型 kb 自動(dòng)轉(zhuǎn)成bm、g、tb#} p>文件大小{{ file_size|filesizeformat }}/p> {# 切片 從指定位置到指定位 ,例:第3位到-2位#} p>切片:{{ slice_str|slice:'3:-2' }}/p> {# 把datetime取到的時(shí)間,轉(zhuǎn)成指定格式#} p>格式化:{{ now|date:'Y-m-d H:i:s' }}/p> {# 如果后端內(nèi)容包含標(biāo)簽,那么加上safe 才能轉(zhuǎn)義(防止用戶直接加script標(biāo)簽作弊)防XSS攻擊#} p>{{ h_html|safe }}/p> {# 取摘要只顯示一段,指定取長(zhǎng)度后面...例:120個(gè)字符 #} p>長(zhǎng)文本:{{ p_str|truncatechars:12 }}/p>
1、視圖
class UserView(ListAPIView): """用戶列表""" queryset = User.objects.all() serializer_class = UserSerializer filter_backends = (DjangoFilterBackend,) filter_class = UserMonthFilter # 指定過濾類
2、過濾類
class RobotFilter(django_filters.FilterSet): # 使用過濾:URL?created_start_time=2020_01-20created_end_time=2020_01-21 robot_id = django_filters.CharFilter(field_name='id') machine_id = django_filters.CharFilter(field_name='machine_id') city = django_filters.CharFilter(field_name='city') # lookup_expr(可選)為判斷條件,field_name(必選)為模型類屬性,created_time查詢字符串 created_time= django_filters.CharFilter(field_name='created_at', lookup_expr='startswith') created_start_time = django_filters.DateTimeFilter(field_name='created_at', lookup_expr='gt') created_end_time = django_filters.DateTimeFilter(field_name='created_at', lookup_expr='lt') problem_isnull = django_filters.BooleanFilter(field_name='problem', lookup_expr='isnull') name = django_filters.CharFilter(lookup_expr='iexact') # iexact表示精確匹配, 并且忽略大小寫 author = django_filters.CharFilter(lookup_expr='icontains') #icontains表示模糊查詢(包含),并且忽略大小寫 price = django_filters.NumberFilter(look_expr='exact') #exact表示精確匹配 task_res_state = django_filters.CharFilter(method="get_task_res_state") def get_task_res_state(self, queryset, *arg): if str(arg[1]) == "0": # arg[1]=('task_res_state', '0') task_res = (1, 2, 3) else: task_res = (0, 4, 5, 6) print(task_res) queryset = queryset.filter(task_res__in=task_res) return queryset class Meta: model = Robot fields = ['robot_id', 'machine_id', "city", "created_start_time", "created_end_time", 'created_time', 'firmware_version', 'state', "robot_type", "hardware_version", "exist_map", 'task_res_state']
到此這篇關(guān)于django filters實(shí)現(xiàn)數(shù)據(jù)過濾的示例代碼的文章就介紹到這了,更多相關(guān)django filters 數(shù)據(jù)過濾 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:惠州 上海 鷹潭 常德 黑龍江 四川 黔西 益陽(yáng)
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《django filters實(shí)現(xiàn)數(shù)據(jù)過濾的示例代碼》,本文關(guān)鍵詞 django,filters,實(shí)現(xiàn),數(shù)據(jù),過濾,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。