django filters實(shí)現(xiàn)數(shù)據(jù)過(guò)濾的示例代碼
常用
當(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ù)#} {# 如果沒(méi)有值,那么使用默認(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 # 指定過(guò)濾類
2、過(guò)濾類
class RobotFilter(django_filters.FilterSet): # 使用過(guò)濾:URL?created_start_time=2020_01-20&created_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ò)濾的示例代碼的文章就介紹到這了,更多相關(guān)django filters 數(shù)據(jù)過(guò)濾 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python安裝后無(wú)法打開IDLE?Subprocess?Connection?Error的解決方法
有朋友在安裝了Python之后發(fā)現(xiàn)不能正常使用,就說(shuō)明安裝過(guò)程出了問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于python安裝后無(wú)法打開IDLE?Subprocess?Connection?Error的解決方法,需要的朋友可以參考下2023-01-01python實(shí)現(xiàn)QQ郵箱群發(fā)郵件實(shí)例
大家好,本篇文章主要講的是python實(shí)現(xiàn)QQ郵箱群發(fā)郵件實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01Python操作MySQL數(shù)據(jù)庫(kù)的入門指南
MySQL是一種關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),廣泛應(yīng)用于各種應(yīng)用程序和網(wǎng)站,在本篇技術(shù)博客中,我們將探討如何使用Python操作MySQL數(shù)據(jù)庫(kù),需要的可以收藏一下2023-06-06Python實(shí)現(xiàn)比較撲克牌大小程序代碼示例
這篇文章主要介紹了Python實(shí)現(xiàn)比較撲克牌大小程序代碼示例,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12使用matplotlib繪制熱圖(heatmap)全過(guò)程
這篇文章主要介紹了使用matplotlib繪制熱圖(heatmap)全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12解決django服務(wù)器重啟端口被占用的問(wèn)題
今天小編就為大家分享一篇解決django服務(wù)器重啟端口被占用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-0730?個(gè)?Python?函數(shù),加速數(shù)據(jù)分析處理速度
這篇文章主要介紹了30?個(gè)?Python?函數(shù),加速數(shù)據(jù)分析處理速度,Pandas?是?Python?中最廣泛使用的數(shù)據(jù)分析和操作庫(kù)。它提供了許多功能和方法,可以加快數(shù)據(jù)分析和預(yù)處理步驟,下面我們就一起來(lái)看看這些方法吧,需要的小伙伴可以參考一下,希望給你帶來(lái)幫助2021-12-12