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 ...
随机推荐
- iOS开发:自定义tableViewCell处理的问题
还在适配iOS6,索性下一个版本不适配了~~~~~ 问题: *** Assertion failure in -[ PCDiaryDetailReplyCell layoutSublayersOfLa ...
- PSAM 卡的应用操作方法
PSAM 卡的应用 PSAM 功能 终端安全存储模块 PASM 常用于 脱机交易的 安全认证 脱机交易的流程 1.卡片对持卡人的认证(防止 ...
- javascript 中ASCII字符值转换
char-->ascii var a = "123"; a.charAt(1).charCodeAt();ascii-->char String.fro ...
- 在docker以FPM-PHP运行php,慢日志导致的BUG分析
问题描述: 最近将IOS书城容器化,切换流量后.正常的业务测试了一般,都没发现问题.线上的错误监控系统也没有报警,以为迁移工作又告一段落了,暗暗的松了一口气.紧接着,报警邮件来了,查看发现是一个苹果支 ...
- 【No system images installed for this target】的解决方式
打开eclipse,新建安卓SDK模拟器时,选择完Target之后,再选择CPU/ABI时,默认为No system images installed for this target. 且无法编辑: ...
- bzoj1681[Usaco2005 Mar]Checking an Alibi 不在场的证明
Description A crime has been comitted: a load of grain has been taken from the barn by one of FJ's c ...
- Polymorphism & Overloading & Overriding
In Java, a method signature is the method name and the number and type of its parameters. Return typ ...
- 齐B小短裙
女模周蕊微博引关注 火了齐B小短裙毁了干爹[图]_网易新闻中心 齐B小短裙
- 关于void*函数返回
一. sample #include<iostream> using namespace std; void* test(void* pass) { return pass; } int ...
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 方法execut ...