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来筛选你的数据的更多相关文章

  1. Django - Xadmin (四) Filter

    Django - Xadmin (四) Filter Filter 功能描述 与 admin 组件中 Filter 功能类似,在展示页面右侧放置一列标签,通过点击这些标签来筛选出该标签相关的数据. 比 ...

  2. django中的filter和get的区别 (MultipleObjectsReturned: get() returned more than one Publisher --)(DoesNotExist: Publisher matching query does not exist.)

    上面的例子中`` filter()`` 函数返回一个记录集,这个记录集是一个列表. 相对列表来说,有些时候我们更需要获取单个的对象, `` get()`` 方法就是在此时使用的: >>&g ...

  3. Django中通过filter和simple_tag为前端实现自定义函数

    Django的模板引擎提供了一般性的功能函数,通过前端可以实现多数的代码逻辑功能,这里称之为一般性,是因为它仅支持大多数常见情况下的函数功能,例如if判断,ifequal对比返回值等,但是稍微复杂一些 ...

  4. Django MTV simple_tag filter inclusion_tag

    Django框架 模型(Model).视图(View)和控制器(Controller),具有耦合性低.重用性高.生命周期成本低等优点. MVC 框架 --  Model -View -Controll ...

  5. Django中利用filter与simple_tag为前端自定义函数的实现方法

    转自:http://www.jb51.net/article/116303.htm 前言 Django的模板引擎提供了一般性的功能函数,通过前端可以实现多数的代码逻辑功能,这里称之为一般性,是因为它仅 ...

  6. cxgrid对经过筛选过的数据的选择(反选)

    // 下面这个主要是对查询出来的数据, 经过筛选后得到的数据中进行反选操作 ,然后对选择的数据进行修改(全选或选择一部分也可以根据些代码修改) Screen.Cursor := crHourGlass ...

  7. django之创建第8-3个项目-数据库数据提取之高级操作

    1.配置test2.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  8. Django 08 Django模型基础3(关系表的数据操作、表关联对象的访问、多表查询、聚合、分组、F、Q查询)

    Django 08 Django模型基础3(关系表的数据操作.表关联对象的访问.多表查询.聚合.分组.F.Q查询) 一.关系表的数据操作 #为了能方便学习,我们进入项目的idle中去执行我们的操作,通 ...

  9. django 简易博客开发 2 模板和数据查询

    首先还是贴一下项目地址  https://github.com/goodspeedcheng/sblog   因为代码全在上面 上一篇博客我们介绍了 django的安装配置,新建project,新建a ...

  10. aspnet中通过多条件筛选来显示数据的实现

    UI图: 功能实现: 1.勾选住哪个选项之后,就加入了筛选.支持姓名的模糊查询. 2.对筛选出来的数据可以直接修改,并更新回数据库. 说明:显示的数据来自T_User表.数据显示控件使用的是 List ...

随机推荐

  1. 类和对象:面向对象编程 - 零基础入门学习Python037

    类和对象:面向对象编程 让编程改变世界 Change the world by program 经过上节课的热身,相信大家对类和对象已经有了初步的认识,但似乎还是懵懵懂懂:好像面向对象编程很厉害,但不 ...

  2. 所谓has a 和 is a

    在 C# 中 很好理解: {  is    a: 继承关系.    has a: 成员关系,其他类是本类的成员.} 在C++ 中稍微复杂一点: {  由于有多重继承, 继承也可能是has a,类似C# ...

  3. C 语言简历一个文件夹 并自己输入字符 来取文件夹名字

    int main(void) { FILE *fp; char ch,filename[10]; scanf("%s",filename); if((fp=fopen(filena ...

  4. 成为IT精英,我奋斗了7年

    成为IT精英,我奋斗了7年 这些日子 我一直在写一个实时操作系统内核,已有小成了,等写完我会全部公开,希望能够为国内IT的发展尽自己一份微薄的力量.最近看到很多学生朋友和我当年一样没有方向 ,所以把我 ...

  5. Android系统服务-简介

    http://blog.csdn.net/chenyafei617/article/details/6577907 Introduction 我们知道Android系统服务挺多的,做程序时经常会用到, ...

  6. 把第三方的exe程序嵌入C#界面上

    public partial class eTerm_Form : WinFormsUI.Docking.DockContent{public eTerm_Form(){InitializeCompo ...

  7. (转)iOS Wow体验 - 第二章 - iOS用户体验解析(1)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第二章译文精选的第一部分,其余章节将陆续放出.上一 ...

  8. linux高级技巧:heartbeat+lvs(一)

    1.heartbeat一个简短的引论:        Heartbeat 项目是 Linux-HA project的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组 ...

  9. UICollectionView的基本使用

    这个控件,看起来与UITableView有点像,而且基本的用法也很相像哦!!! 我们来看看API: #pragma mark - UICollectionViewDataSource // 指定Sec ...

  10. ARM体系结构_DAY2

    程序状态寄存器(CPSR) Mode位[4:0]:处理器模式为 USER模式不能直接切换到特权模式,在特权模式下可以直接修改mode位[4:0]为10000,切换到USER模式. T bit位[5]: ...