django包服务器端处理数据表
sspdatatables的Python项目详细描述
简介
背景
在django项目中,许多表用于显示存储的信息 在数据库中。但是,随着时间的推移,数据库越来越大,比如 表需要更多的时间来加载内容。在这种情况下,减少 加载时间成为软件开发人员最重要的任务。 常用的方法是使用服务器端处理表,它只加载 每次都有一小部分信息。
要使用服务器端处理表,我们有两个选项:要么开发 一个新的,或者我们只是用别人实现的。对于第一个, 我们必须投入大量的时间和精力,除此之外,我们还要确保 可以在任何地方使用。那对我来说很难,也没那么有趣 (考虑javascript代码)。第二种选择,实际上我们只需要 定制它。这很容易实现。
数据表是一个javascript包,它提供 很多不错的功能包括服务器端处理。但是,使用它 在Django项目中并不容易。为了简化它的使用,我决定实现 一个小包裹。
简介
sspdatatables是django项目的python包。它使用 数据表包并提供一个不错的 Django项目友好界面,用于实现服务器端处理表。
使用此包,您不仅可以实现可筛选、可订购和服务器 侧面加工台。在一般情况下,您甚至不需要定义查询 功能,我已经为您做了。
包装结构
sspdatatables
| __init__.py
| apps.py
| datatables.py
| forms.py
|
|---utils
| | __init__.py
| | decorator.py
| | enum.py
| | data_type_ensure.py
|
|---templates
| |
| |---datatables
| | |
| | |---html
| | | table.html
| | |
| | |---js
| | | footer.js
| | | general.js
| |
|---templatetags
| __init__.py
| form_field.py
|
|---tests
| __init__.py
| test_data_type_ensure_doctest.txt
| test_enum_doctest.txt
如何使用
设置
- 运行pip install sspdatatables
- 在settings.py中的
已安装的应用程序中添加
并指定格式 (sspdatatables
- 在设置中的
模板的
目录中添加
sspdatatables/templates/
。 < > >教程
在本节中,我将使用一个示例项目来解释如何使用sspdatatables
项目sspdatatables示例:
结构:
sspdatatablesExample | manage.py | manuals.py | requirements.txt | |---example | | __init__.py | | admin.py | | apps.py | | models.py | | tests.py | | urls.py | | views.py | | | |---migrations | | | __init__.py | | | 0001_initial.py | | | |---templates | | index.html | | overview.html | |---sspdatatablesExample | __init__.py | admin.py | apps.py | settings.py | tests.py | urls.py | wsgi.py | |---migrations | __init__.py
示例中的models.py文件的内容
from django.db import models from django_countries.fields import CountryField class Author(models.Model): name = models.CharField(max_length=60) nationality = CountryField() class Book(models.Model): name = models.CharField(max_length=60) description = models.TextField() author = models.ForeignKey(Author, on_delete=None) published_at = models.DateField(auto_now=True)
我们要显示名称,已发布的,作者姓名,
作者国籍书籍信息。我们的桌子应该是:
<表><广告> < /广告><正文>动作 名称 作者 作者国籍 发表于 :铅笔: 预订A 第1个人 国家1 2000年7月18日 < Buff行情>:铅笔: B册 第二个人 国家2 1999年7月18日 注意:
- 在datatables中,列号是0-index,这意味着column操作是 实际上,column0,columnname是column1
在了解了我们的需求之后,我们就可以开始实施它了。
步骤:
步骤1.
在app示例中创建文件夹datatables并添加空文件: in it.py,datatables.py,enums.py,forms.py,序列化程序.py在其中 < Buff行情>
说明:
- sspdatatables必需文件通常不被其他文件使用,因此 将它们组合在一起有助于保持项目的结构清晰 简单(至少用于sspdatatables)。
第2步。
在文件enums.py中输入以下代码:
fromsspdatatables.utils.enumimportTripleEnumclassBookEnum(TripleEnum):ID=(1,"id","id")NAME=(2,"name","name__icontains")AUTHOR_NAME=(3,"author__name","author__name__icontains")AUTHOR_NATIONALITY=(4,"author__nationality","author__nationality")PUBLISHED_AT=(5,"published_at","published_at")
< Buff行情>说明:
- 首先,我们需要从
sspdatatables
导入tripleenum
。然后我们 为它创建一个子类来定义一个映射,这是一个3元素元组(即 为什么枚举类称为triplenum
)。 - 映射的第一个元素表示表中的列号。 第二个元素是model类中定义的相应字段名, 第三个元素是字段的筛选键。
- 创建这个枚举类的目的是 列号我们可以使用相应的字段名 内容和相应的筛选键来筛选内容。
步骤3。
在fileserializers.py中输入以下代码:
fromexample.modelsimportBook,Authorfromrest_frameworkimportserializersfromdjango_countries.serializersimportCountryFieldMixinfromdjango_countries.serializer_fieldsimportCountryFieldclassAuthorSerializer(CountryFieldMixin,serializers.ModelSerializer):nationality=CountryField(country_dict=True)classMeta:model=Authorfields=('name','nationality')classBookSerializer(serializers.ModelSerializer):author=AuthorSerializer(read_only=True)published_at=serializers.DateField(format='%b. %d, %Y',required=False)classMeta:model=Bookfields=('id','name','published_at','author')
< Buff行情>说明:
我们在这里要做的实际上是为表定义modelserializer。 创建两个ModelSerializer有两个原因(一个用于Author,一个用于Book):
- 要访问外键引用记录的信息,必须 为它定义一个模型序列化器。这是由django rest框架定义的
- 我们不仅要展示这本书的信息,还要展示它的作者 信息,并且作者在book中被作为外键引用 < > >
- 如果在model类中使用countryfield,并且希望显示 国家名称而不是表中的国家代码
注意:
authorserizer
类中为国籍定义的方法相同 (在序列化数据中,您可以使用item.national.name
)。- 如果要以特定格式显示日期,必须使用 此日期字段的名称相同,用于声明新的序列化程序。日期字段
序列化程序.datefield
对应于模型.datefield
和序列化程序.datetimefield
对应于模型.datetimefield
)。 - 在设置中的
- 类
datatables
用作连接显示表和数据库的桥梁:- 根据表的内部变量
结构配置表的框架
- 序列化DB中的记录以供显示(变量
序列化程序
)
- 在帮助下将表提供的筛选条件应用于数据库 其内部变量的
- 将表尾的搜索字段自定义为选择字段,而不是默认的输入字段(变量
窗体
)
- 序列化DB中的记录以供显示(变量
- 根据表的内部变量
- 数据表的用法非常简单,只需创建一个子类 并定义其内部元类的变量。
- 到目前为止,我们只希望有一个带有页脚搜索字段的表(以input的形式)。 最后,我将向您展示如何将页脚搜索字段更改为选择类型。
- 如果您的表有一个复杂的查询,并且您希望自定义该查询
函数,您可以重新定义函数
get_query_dict
,get_order_key
,筛选
,切片
,按参数筛选
或过程
在定义的内部 数据表的子类。在这种情况下,您需要先阅读源代码。 - 通过函数
get_dt_elements
可以获得帧(返回 函数的结果是一个只包含一个元素的字典 键-值对),还可以扩展上下文 呈现更多变量
- 通过decorator
确保ajax
我们只能确保ajax请求 允许的请求方法被views函数接受。
- 函数
过程的
预搜索条件
参数必须是 订购信息技术类型: -
pre_search_condition
中的键是modelmanager中的函数名 模型类"book"的值,例如selected_related
,filter
,values
等。 -pre_search_condition
中的值是相应函数的参数' 价值观。例如: +如果使用select_related
作为键,则可以输入单个值作为 相关的数据库表或值列表。 +如果使用filter
作为键,则其值必须是字典,在 其中一个键值对表示一个过滤键及其对应的过滤值, 例如ordereddict([('filter',{'name\uu icontains','django'}))
数据表
类将迭代预搜索条件
并应用 这些条件按顺序排列。- 由于
author
是book
模型类中的外键,我们需要设置在
搜索前条件中选择与搜索相关的
在
sspdatatablesExample
project i create中为表准备一个html文件 一个名为overview.html的html文件,位于app
example
的
templates
文件夹中。 如下所示:
0sspdatatables | __init__.py | apps.py | datatables.py | forms.py | |---utils | | __init__.py | | decorator.py | | enum.py | | data_type_ensure.py | |---templates | | | |---datatables | | | | | |---html | | | table.html | | | | | |---js | | | footer.js | | | general.js | | |---templatetags | __init__.py | form_field.py | |---tests | __init__.py | test_data_type_ensure_doctest.txt | test_enum_doctest.txt
在
块内容中包含来自
sspdatatables
,并在块脚本中包含
datatables/js/general.js
从sspdatatables
。现在overview.html
应该是这样的:
1sspdatatables | __init__.py | apps.py | datatables.py | forms.py | |---utils | | __init__.py | | decorator.py | | enum.py | | data_type_ensure.py | |---templates | | | |---datatables | | | | | |---html | | | table.html | | | | | |---js | | | footer.js | | | general.js | | |---templatetags | __init__.py | form_field.py | |---tests | __init__.py | test_data_type_ensure_doctest.txt | test_enum_doctest.txt
在
块脚本中用设置声明
数据表的实例 你想要(这里我会提供一些常用的设置)。
overview.html变成:
2sspdatatables | __init__.py | apps.py | datatables.py | forms.py | |---utils | | __init__.py | | decorator.py | | enum.py | | data_type_ensure.py | |---templates | | | |---datatables | | | | | |---html | | | table.html | | | | | |---js | | | footer.js | | | general.js | | |---templatetags | __init__.py | form_field.py | |---tests | __init__.py | test_data_type_ensure_doctest.txt | test_enum_doctest.txt
< > >- 在内部的meta类中,将变量字段定义为2个元素的列表元组:
- 第一个元素是列的id(在变量中定义
结构
类的
bookdatatables
在datatables.py
文件中) - 第二个元素是它对应的页脚字段类型(当前
它只支持
choicefield
)。;
- 第一个元素是列的id(在变量中定义
- 为
字段中的每个元素定义一个函数。函数的名称 必须是元组选项中的第一个元素。这个函数返回 以元组列表的形式选择此选择字段。
< > > - 将类bookdatatables的variable
form
的值设置为form类 我们刚刚定义了bookfieldselectform - 在变量
structure
中查找相应列的帧定义 classbookdatatables
,将keyfooter\u type的值更改为
'select'
< > >
有关django rest framework的更多信息,请访问http://www.django-rest-framework.org/tutorial" rel="nofollow">http://www.django-rest-framework.org/tutorial
步骤4.
在filedatatables.py中输入以下代码:
fromsspdatatables.datatablesimportDataTablesfrom.serializersimportBookSerializerfrom.enumsimportBookEnumfrom.formsimportBookFieldSelectFormclassBookDataTables(DataTables):classMeta:serializer=BookSerializerform=Nonestructure=[{"id":"actions","serializer_key":None,"header":"Actions","searchable":False,"orderable":False,"footer_type":None,},{"id":"id","serializer_key":'id',"header":"ID","searchable":True,"orderable":True,"footer_type":"input",},{"id":"name","serializer_key":'name',"header":"Name","searchable":True,"orderable":True,"footer_type":"input",},{"id":"author","serializer_key":'author.name',"header":"Author","searchable":True,"orderable":True,"footer_type":"input",},{"id":"author_nationality","serializer_key":'author.nationality.name',"header":"Author Nationality","searchable":True,"orderable":True,"footer_type":"input",},{"id":"published_at","serializer_key":'published_at',"header":"Published At","searchable":True,"orderable":True,"footer_type":"input","placeholder":"YYYY-MM-DD",},]col_triple_enum=BookEnum< Buff行情>
说明:
注意:
步骤5.
到目前为止我们lReady设置后端部分以使用sspdatatables
。在这一步骤中
我们开始处理前端部分。
需要两个视图函数:一个用于渲染表的框架,另一个用于 一种是加载表的内容。
首先,在应用程序示例的view.py文件中创建first views函数 用于显示表格的框架:
fromdjango.shortcutsimportrenderfromdjango.httpimportJsonResponsefrom.datatablesimportBookDataTablesfromsspdatatables.utils.decoratorimportensure_ajaxfromcollectionsimportOrderedDictdefoverview(request):book_datatables=BookDataTables()context=book_datatables.get_dt_elements()context.update({"title":"Books",})returnrender(request,'overview.html',context)< Buff行情>
说明:
其次,仍然在此文件中创建用于加载内容的第二个视图功能:
@ensure_ajax(['GET'])defget_book_api(request):pre_search_condition=OrderedDict([('select_related','author')])book_datatables=BookDataTables()result=book_datatables.process(pre_search_condition=pre_search_condition,**request.GET)returnJsonResponse(result)< Buff行情>
说明:
第三,定义两个视图的url函数,如:
fromdjango.conf.urlsimporturlfromexample.viewsimportoverview,get_book_apiurlpatterns=[url(r'^books/$',overview,name='book_overview'),url(r'^api/$',get_book_api,name='book_api'),]
步骤6。
在这一步中,我们将编写一些html代码和javascript代码。
您可以阅读datatable
网站中的文档,了解javascript设置。
此时,我们已经实现了model类的out table。如果你 想知道如何自定义页脚搜索字段,您可以进入下一步。
步骤7。
在appexample
的文件夹
数据表中的文件
forms.py
中输入以下代码:
sspdatatables
| __init__.py
| apps.py
| datatables.py
| forms.py
|
|---utils
| | __init__.py
| | decorator.py
| | enum.py
| | data_type_ensure.py
|
|---templates
| |
| |---datatables
| | |
| | |---html
| | | table.html
| | |
| | |---js
| | | footer.js
| | | general.js
| |
|---templatetags
| __init__.py
| form_field.py
|
|---tests
| __init__.py
| test_data_type_ensure_doctest.txt
| test_enum_doctest.txt
3
< Buff行情>说明:
在sspdatatables
中有一个名为abstractfooterform
的表单类
简化页脚搜索字段的窗体类的定义。您需要做的是
之后,只剩下两个小步骤,转到datatables.py
文件
datatables.py
文件应该是:
sspdatatables
| __init__.py
| apps.py
| datatables.py
| forms.py
|
|---utils
| | __init__.py
| | decorator.py
| | enum.py
| | data_type_ensure.py
|
|---templates
| |
| |---datatables
| | |
| | |---html
| | | table.html
| | |
| | |---js
| | | footer.js
| | | general.js
| |
|---templatetags
| __init__.py
| form_field.py
|
|---tests
| __init__.py
| test_data_type_ensure_doctest.txt
| test_enum_doctest.txt
4
享受!sspdatatablesample
的源代码包含在包的example文件夹中。