django-filter 使用Filter来筛选你的数据
django-filter##
Django-filter is a generic, reusable application to alleviate writing some of the more mundane bits of view code. Specifically, it allows users to filter down a queryset based on a model’s fields, displaying the form to let them do this.
这是一个用来筛选要显示数据集的工具,在上一篇的restframework中,这个应用也是可选的插件。所以这篇来看看这个app的应用。
文档:https://django-filter.readthedocs.org/en/latest/usage.html
安装:###
pip install django-filter'
接着把 'django_filters' 添加到 INSTALLED_APPS.
比如我们要使用filter来筛选上一篇中的项目post对象。
首先, 新建一个post/filters.py来保存我们的filter
import django_filters
from .models import Post
class PostFilter(django_filters.FilterSet):
class Meta:
model = Post
然后在views.py中添加对应的视图:
from .filters import PostFilter
...
def post_list(request):
f = PostFilter(request.GET, queryset=Post.objects.all())
return render(request, 'post/index.html', { 'filter':f })
建立视图模板文件templates/post/index.html
<form action="" method="get">
{{ filter.form.as_p }}
<input type="submit" />
</form>
{% for obj in filter %}
{{ obj.title }} - {{ obj.content }} - ${{ obj.pub_date }}<br>
{% endfor %}
注意到这个模板中,用一个form来提交要filter的内容。然后结果显示在下面.
然后打开浏览器 localhost:8000/post/list
就可以看到这个了。
注意到我们的PostFilter并没有设置什么,那么就默认会把Post类的所有属性列出来。
如果我们只想筛选title, 那么在Meta类中添加fields属性:
class PostFilter(django_filters.FilterSet):
class Meta:
model = Post
fields = ['title']
但是title现在的匹配是区分大小写的,如果要改变这个特性,那么添加一个filter:
class PostFilter(django_filters.FilterSet):
title = django_filters.CharFilter(name='title', lookup_expr='iexact')
比如我们像筛选pub_date在某年之前:
publish_date_year_before = django_filters.NumberFilter(name='pub_date', lookup_expr='year__lt')
这样就会在页面显示publish date year before这个input了。
filter除了CharFilter, NumberFilter,还可以是MethodFilter
title = djang_filters.MethodFilter(name='title', action='my_custom_filter')
...
def my_custom_filter(self, queryset, value):
return queryset.filter(
title = value
)
要更详细参考文档。
django-filter 使用Filter来筛选你的数据的更多相关文章
- Django - Xadmin (四) Filter
Django - Xadmin (四) Filter Filter 功能描述 与 admin 组件中 Filter 功能类似,在展示页面右侧放置一列标签,通过点击这些标签来筛选出该标签相关的数据. 比 ...
- django中的filter和get的区别 (MultipleObjectsReturned: get() returned more than one Publisher --)(DoesNotExist: Publisher matching query does not exist.)
上面的例子中`` filter()`` 函数返回一个记录集,这个记录集是一个列表. 相对列表来说,有些时候我们更需要获取单个的对象, `` get()`` 方法就是在此时使用的: >>&g ...
- Django中通过filter和simple_tag为前端实现自定义函数
Django的模板引擎提供了一般性的功能函数,通过前端可以实现多数的代码逻辑功能,这里称之为一般性,是因为它仅支持大多数常见情况下的函数功能,例如if判断,ifequal对比返回值等,但是稍微复杂一些 ...
- Django MTV simple_tag filter inclusion_tag
Django框架 模型(Model).视图(View)和控制器(Controller),具有耦合性低.重用性高.生命周期成本低等优点. MVC 框架 -- Model -View -Controll ...
- Django中利用filter与simple_tag为前端自定义函数的实现方法
转自:http://www.jb51.net/article/116303.htm 前言 Django的模板引擎提供了一般性的功能函数,通过前端可以实现多数的代码逻辑功能,这里称之为一般性,是因为它仅 ...
- cxgrid对经过筛选过的数据的选择(反选)
// 下面这个主要是对查询出来的数据, 经过筛选后得到的数据中进行反选操作 ,然后对选择的数据进行修改(全选或选择一部分也可以根据些代码修改) Screen.Cursor := crHourGlass ...
- django之创建第8-3个项目-数据库数据提取之高级操作
1.配置test2.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- Django 08 Django模型基础3(关系表的数据操作、表关联对象的访问、多表查询、聚合、分组、F、Q查询)
Django 08 Django模型基础3(关系表的数据操作.表关联对象的访问.多表查询.聚合.分组.F.Q查询) 一.关系表的数据操作 #为了能方便学习,我们进入项目的idle中去执行我们的操作,通 ...
- django 简易博客开发 2 模板和数据查询
首先还是贴一下项目地址 https://github.com/goodspeedcheng/sblog 因为代码全在上面 上一篇博客我们介绍了 django的安装配置,新建project,新建a ...
- aspnet中通过多条件筛选来显示数据的实现
UI图: 功能实现: 1.勾选住哪个选项之后,就加入了筛选.支持姓名的模糊查询. 2.对筛选出来的数据可以直接修改,并更新回数据库. 说明:显示的数据来自T_User表.数据显示控件使用的是 List ...
随机推荐
- python 解决递归调用栈溢出
递归函数 2578次阅读 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact ...
- 钢管下料问题(钢管用量最少)Lingo求解
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang !钢管下料问题1(钢管用量最少) 题目: 钢管原料 每根19m 客户需求 4m 50根 ...
- Ubuntu 怎么在右键添加打开终端
方法一: 搜索nautilus-open-terminal安装 命令行:sudo apt-get install nautilus-open-terminal (如果提示为找的什么的就s ...
- 如何在Protel99se中批量修改元件的封装
有时候需要批量修改元件的封装,可在原理图和PCB中批量修改.本文以批量修改电阻AXIAL0.3 的封装为AXIAL0.4 为例. 1. 在原理图中批量修改1.1. 方法1双击需要修改封装的其中一个元件 ...
- [VBA] excel获取单元格的超链接地址函数
Function geturl(c As Range) As String geturl = c.Hyperlinks().Address End Function 设置超链接的函数是HYPERLIN ...
- 基于PCA和SVM的人脸识别系统-error修改
------------------------------------------------- Undefined function or variable 'W'. Error in class ...
- Spring的工作原理核心组件和应用
Spring框架 Spring 是管理多个java类的容器框架,注意是类不管理接口. Spring 的主要功能 Ioc 反转控制和 DI 依赖注入. 注入的方式可以是构造函数赋值也可以是 set方法赋 ...
- html a标签打开邮件
<a href="mailto:frotech@foxmail.com" target="_blank">frotech@foxmail.com&l ...
- ZigBee心电传输(一)
第一次接触模拟的东西哈,也算是一次新的学习旅程以及对ZigBee的再一次探索吧. 首先是方案制定,以及采用芯片AD8232,这样节省了不少时间,把模拟的东西都搬到数字上了,不过还是需要学习不少模电知识 ...
- [Javascript] Advanced Reduce: Composing Functions with Reduce
Learn how to use array reduction to create functional pipelines by composing arrays of functions. co ...