Django RQL过滤

django-rql的Python项目详细描述


Django RQL公司

pyversionsPyPi StatusDocscodecovBuild StatusPyPI statusQuality Gate Status

django-rql是一个Django应用程序,它为您的web应用程序实现RQL过滤器后端。在

RQL公司

RQL(资源查询语言)是为现代应用程序开发而设计的。它是为web构建的,为NoSQL做好了准备,并且具有高度的可扩展性,语法简单。 这是一种查询语言,快速方便的数据库交互。RQL是为在url中请求对象样式的数据结构而设计的。在

RQL Reference

RQL for Web

注意事项

解析是用Larkcheatsheet)完成的。 当前的解析算法是使用标准lexer的LALR(1)。在

当前支持的运算符

  1. 比较(eq、ne、gt、ge、lt、le、like、ilike、搜索)
  2. 列表(输入、输出)
  3. 逻辑(and,or,not)
  4. 常量(null(),empty())
  5. 订货(订货)
  6. 选择(Select)

文件

完整的文档可在https://django-rql.readthedocs.org上找到。在

示例

fromdj_rql.constantsimportFilterLookupsfromdj_rql.filter_clsimportRQLFilterClass,RQL_NULLclassModelFilterClass(RQLFilterClass):"""    MODEL - Django ORM model    FILTERS - List of filters    EXTENDED_SEARCH_ORM_ROUTES - List of additional Django ORM fields for search    DISTINCT - Boolean flag, that specifies if queryset must always be DISTINCT    SELECT - Boolean flag, that specifies if Filter Class supports select operations and queryset optimizations    OPENAPI_SPECIFICATION - Python class that renders OpenAPI specification    Filters can be set in two ways:        1) string (default settings are calculated from ORM)        2) dict (overriding settings for specific cases)    Filter Dict Structure    {        'filter': str        # or        'namespace': str        'source': str        # or        'sources': iterable        # or        'custom': bool        # or        'dynamic': bool        'field': obj        'lookups': set        'qs': obj        'use_repr': bool  # can't be used in namespaces        'ordering': bool  # can't be true if 'use_repr=True'        'search': bool    # can't be true if 'use_repr=True'        'hidden': bool    }    """MODEL=ModelFILTERS=['id',{# `null_values` can be set to override ORM is_null behaviour# RQL_NULL is the default value if NULL lookup is supported by field'filter':'title','null_values':{RQL_NULL,'NULL_ID'},'ordering':False,},{# `ordering` can be set to True, if filter must support ordering (sorting)# `ordering` can't be applied to non-db fields'filter':'status','ordering':True,},{# `search` must be set to True for filter to be used in searching# `search` must be applied only to text db-fields, which have ilike lookup'filter':'author__email','search':True,},{# `source` must be set when filter name doesn't match ORM path'filter':'name','source':'author__name',},{# `namespace` is useful for API consistency, when dealing with related models'namespace':'author','filters':['id','name'],# will be converted to `author.id` and `author.name`},{# `distinct` needs to be setup for filters that require QS to work in DISTINCT mode# `openapi` configuration is automatically collected by OpenAPI autogenerator'filter':'published.at','source':'published_at','distinct':True,'openapi':{'required':True,'deprecated':True,'description':'Good description','hidden':False,# can be set to avoid collecting by autogenerator# type and format are collected automatically and shouldn't be setup, in general'type':'string','format':'date',},},{# `use_repr` flag is used to filter by choice representations'filter':'rating.blog','source':'blog_rating','use_repr':True,},{# `hidden` flag is used to set default select behaviour for associated field'filter':'rating.blog_int','source':'blog_rating','use_repr':False,'ordering':True,'hidden':True,},{# We can change default lookups for a certain filter'filter':'amazon_rating','lookups':{FilterLookups.GE,FilterLookups.LT},},{# Sometimes it's needed to filter by several sources at once (distinct is always True).# F.e. this could be helpful for searching.'filter':'d_id','sources':{'id','author__id'},'ordering':True,},{# Some fields may have no DB representation or non-typical ORM filtering# `custom` option must be set to True for such fields'filter':'custom_filter','custom':True,'lookups':{FilterLookups.EQ,FilterLookups.IN,FilterLookups.I_LIKE},'ordering':True,'search':True,'custom_data':[1],}]fromdj_rql.drf.backendimportRQLFilterBackendfromdj_rql.drf.paginationsimportRQLContentRangeLimitOffsetPaginationclassDRFViewSet(mixins.ListModelMixin,GenericViewSet):queryset=MODEL.objects.all()serializer_class=ModelSerializerrql_filter_class=ModelFilterClasspagination_class=RQLContentRangeLimitOffsetPaginationfilter_backends=(RQLFilterBackend,)

注释

  1. 带有空格或特殊字符的值,如“,”需要有“”或“”
  2. 支持的日期格式为ISO8601:2019-02-12
  3. 支持的日期时间格式为ISO8601:2019-02-12T10:02:00/2019-02-12T10:02Z/2019-02-12T10:02:00+03:00
  4. 添加了对来自Django Model Utilities的Choices()字段的支持

Django Rest框架扩展

  1. 分页(限制、偏移)
  2. 支持从基本模型字段(如CharField())继承的任何深度的自定义字段。在
  3. 后端DjangoFiltersRQLFilterBackend,自动将Django-Filters查询转换为RQL查询。在
  4. OpenAPI文档是为过滤器类自动生成的。在

最佳实践

  1. 使用dj_rql.utils.assert_filter_cls测试API视图筛选器。如果映射是正确的,并且没有定制的过滤逻辑,那么实际上可以保证过滤将正确工作。在
  2. 优先使用custom=TrueRQLFilterClass.build_q_for_custom_filter重写而不是重写{}。在
  3. 自定义筛选器可能支持使用build_name_for_custom_ordering排序(ordering=True)。在

发展

  1. Python 3.5+
  2. 安装依赖项requirements/dev.txtrequirements/extra.txt

测试

  1. Python 3.6+
  2. 安装依赖项requirements/test.txt
  3. export PYTHONPATH=/your/path/to/django-rql/

检查代码样式:flake8 运行测试:pytest

测试报告在tests/reports中生成。在

  • out.xml-JUnit测试结果
  • coverage.xml-覆盖率xml结果

要生成HTML覆盖率报告,请使用: --cov-report html:tests/reports/cov_html

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java以编程方式最小化JInternalFrame?   java使用JsonPath将不均匀列表提取为类型化对象   spring如何将java/resources文件获取到InputStream?   Java逻辑错误并不总是存在   java Firebase,更新特定字段   Java stream groupby并同时加入以进行csv导出   java安卓:如果出现任何声音,监听器将录制声音   java如何从多个文件中轮询最后修改的文件并发送到apache camel中的目标端点?   java是否返回多个比较器?   JavaBean IO:Date MMM在CAPS中不解析   当JButton从另一个类单击时,java正在更新JLabel   基类中子类的Java泛型初始化   jakarta ee Java ee制作一个以毫秒为间隔的计时器来发送数据包   json Windows Azure日期格式转换为Java日期