Django 管理员搜索/过滤功能作为页表 [英] Django admin search/filter functionality as a page table

查看:13
本文介绍了Django 管理员搜索/过滤功能作为页表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在常规视图中使用 Django Admin 的过滤/排序/分页/搜索功能的强大功能.

我的意思是我有一个模型,上面有一些字段.我想要一个搜索"表单,其中字段的定义与使用 admin.ModelAdmin 非常相似.用户将能够搜索(使用提供的字段)、按值过滤、对结果表的页面进行分页等.所有这些都不需要我做最少的工作,例如.只需配置表单中应使用哪些字段.像这样的:

class SchoolAdmin(ModelAdmin):list_display = ('id', 'name', )list_display_links = ('名称', )search_fields = ('名字', )list_filter = ('类型', )

有这样的东西吗?还是我必须自己编码?

我需要这样一个插件/应用程序的功能是:

  1. 将数据显示为表格
  2. 按列排序
  3. 过滤(例如仅显示 X = Y 的行")
  4. 按列搜索
  5. 可选配置类似于 ModelAdmin 样式

Alasdair 的 django-tables2 只匹配第一个和第二个条件.

解决方案

你提到的 django 功能在 Django 1.3 的自定义视图中并不是真正可重用的.最近在 django-developers 组上有一些关于 拆分管理功能以使其可重用.

我遇到了两个可能对您有用的项目,django-tables2django-filter.它们都提供略有不同的东西,我认为您正在寻找两者的混合.

django-tables2

<块引用>

django-tables2 简化了将数据集转换为 HTML 表格的任务.它具有对分页和排序的本机支持.它对 HTML 表格的作用与 django.forms 对 HTML 表单的作用一样

django 过滤器

<块引用>

Django-filter 是一个可重用的 Django 应用程序,允许用户过滤动态查询集.它需要 Python 2.4 或更高版本.对于使用和安装说明参考docs目录.

Django-filter 可用于生成类似于 Django 的接口admin 的 list_filter 界面.它有一个与 Django 非常相似的 APIModelForms.

I was wondering if there is a way to use the power of Django Admin's filtering/ordering/paginating/search capabilities in a regular view.

What I mean is that I have a model, some fields on it. I'd like to have a "search" form, where fields would be defined much like using admin.ModelAdmin. User would be able to search (using provided fields), filter by values, paginate through pages of result table etc. All that without minimal amount of work on my part, eg. just configuration which fields should be used in the form. Something like this:

class SchoolAdmin(ModelAdmin):
    list_display = ('id', 'name', )
    list_display_links = ('name', )
    search_fields = ('name', )
    list_filter = ('type', )

Is there something like this available? Or do I have to code it myself?

Edit:

Features I require from such a plugin/application are:

  1. Display data as a table
  2. Sorting by columns
  3. Filtering (eg. "show only rows that has X = Y")
  4. Searching by columns
  5. Optionally configuration similar to ModelAdmin style

Alasdair's django-tables2 only matches 1st and 2nd conditions.

解决方案

The django functionality you mention isn't really reusable in custom views as of Django 1.3. There was recently some discussion on the django-developers group about splitting out admin functionality to make it reusable.

I have come across two projects that might be useful to you, django-tables2 and django-filter. They both offer slightly different things, I think you're looking for a mixture of the two.

django-tables2

django-tables2 simplifies the task of turning sets of data into HTML tables. It has native support for pagination and sorting. It does for HTML tables what django.forms does for HTML forms

django-filter

Django-filter is a reusable Django application for allowing users to filter queryset dynamically. It requires Python 2.4 or higher. For usage and installation instructions consult the docs directory.

Django-filter can be used for generating interfaces similar to the Django admin's list_filter interface. It has an API very similar to Django's ModelForms.

这篇关于Django 管理员搜索/过滤功能作为页表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆