Skip to content

Commit e45c2e0

Browse files
authored
Merge pull request #4 from d3rky/feature/docs-getting-started
Add simple quickstart guide in documentation
2 parents 33b4f50 + ffb772f commit e45c2e0

File tree

2 files changed

+106
-13
lines changed

2 files changed

+106
-13
lines changed

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Django RQL
88
RQL
99
---
1010

11-
RQL (Resource query language) is designed for modern application development. It is built for the web, ready for NoSQL, and highly extensible with simple syntax.
11+
RQL (Resource query language) is designed for modern application development. It is built for the web, ready for NoSQL, and highly extensible with simple syntax.
1212
This is a query language fast and convenient database interaction. RQL was designed for use in URLs to request object-style data structures.
1313

1414

@@ -27,11 +27,16 @@ Currently supported operators
2727
1. Comparison (eq, ne, gt, ge, lt, le, like, ilike, search)
2828
0. List (in, out)
2929
0. Logical (and, or, not)
30-
0. Constants (null(), empty())
30+
0. Constants (null(), empty())
3131
0. Ordering (ordering)
3232
0. Select (select)
3333

3434

35+
Quickstart
36+
==========
37+
Try to enrich your API filtering opportunities learning [Quickstart Guide][quickstart]
38+
39+
3540
Example
3641
=======
3742
```python
@@ -47,17 +52,17 @@ class ModelFilterClass(RQLFilterClass):
4752
DISTINCT - Boolean flag, that specifies if queryset must always be DISTINCT
4853
SELECT - Boolean flag, that specifies if Filter Class supports select operations and queryset optimizations
4954
OPENAPI_SPECIFICATION - Python class that renders OpenAPI specification
50-
55+
5156
Filters can be set in two ways:
5257
1) string (default settings are calculated from ORM)
5358
2) dict (overriding settings for specific cases)
54-
59+
5560
Filter Dict Structure
5661
{
5762
'filter': str
5863
# or
5964
'namespace': str
60-
65+
6166
'source': str
6267
# or
6368
'sources': iterable
@@ -66,17 +71,17 @@ class ModelFilterClass(RQLFilterClass):
6671
# or
6772
'dynamic': bool
6873
'field': obj
69-
74+
7075
'lookups': set
71-
76+
7277
'qs': obj
73-
78+
7479
'use_repr': bool # can't be used in namespaces
7580
'ordering': bool # can't be true if 'use_repr=True'
7681
'search': bool # can't be true if 'use_repr=True'
7782
'hidden': bool
7883
}
79-
84+
8085
"""
8186
MODEL = Model
8287
FILTERS = ['id', {
@@ -105,7 +110,7 @@ class ModelFilterClass(RQLFilterClass):
105110
'filters': ['id', 'name'], # will be converted to `author.id` and `author.name`
106111
},{
107112
# `distinct` needs to be setup for filters that require QS to work in DISTINCT mode
108-
# `openapi` configuration is automatically collected by OpenAPI autogenerator
113+
# `openapi` configuration is automatically collected by OpenAPI autogenerator
109114
'filter': 'published.at',
110115
'source': 'published_at',
111116
'distinct': True,
@@ -148,7 +153,7 @@ class ModelFilterClass(RQLFilterClass):
148153
'lookups': {FilterLookups.EQ, FilterLookups.IN, FilterLookups.I_LIKE},
149154
'ordering': True,
150155
'search': True,
151-
156+
152157
'custom_data': [1],
153158
}]
154159

@@ -177,7 +182,7 @@ Django Rest Framework Extensions
177182
1. Pagination (limit, offset)
178183
0. Support for custom fields, inherited at any depth from basic model fields, like CharField().
179184
0. Backend `DjangoFiltersRQLFilterBackend` with automatic conversion of [Django-Filters](https://django-filter.readthedocs.io/en/master/) query to RQL query.
180-
0. OpenAPI docs are autogenerated for filer classes.
185+
0. OpenAPI docs are autogenerated for filter classes.
181186

182187
Best Practices
183188
==============
@@ -201,9 +206,11 @@ Testing
201206
Check code style: `flake8`
202207
Run tests: `pytest`
203208

204-
Tests reports are generated in `tests/reports`.
209+
Tests reports are generated in `tests/reports`.
205210
* `out.xml` - JUnit test results
206211
* `coverage.xml` - Coverage xml results
207212

208213
To generate HTML coverage reports use:
209214
`--cov-report html:tests/reports/cov_html`
215+
216+
[quickstart]: ./docs/quickstart.md

docs/quickstart.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Quickstart
2+
==========
3+
4+
We're going to create a simple API and configure filters to support RQL syntax.
5+
6+
7+
Installation
8+
------------
9+
Install `django-rql` library in your existing or new Django and Django REST Framework project using command
10+
11+
```
12+
pip install django-rql
13+
```
14+
15+
Configuring Django settings
16+
---------------------------
17+
18+
Setup default `filter_backends` in your Django settings file
19+
20+
```
21+
REST_FRAMEWORK = {
22+
'DEFAULT_FILTER_BACKENDS': ['dj_rql.drf.RQLFilterBackend']
23+
}
24+
```
25+
26+
Now your APIs are supporting RQL syntax for query strings. Let's write some filters
27+
28+
Write your first RQL Filter Class
29+
---------------------------------
30+
31+
For writing your first RQL Filter Class you need some models to be ready. Let's imagine you have simple Domain Model in your project, that can be represented as several models like below
32+
33+
```
34+
from django.db import models
35+
36+
37+
class Product(models.Model):
38+
name = models.CharField()
39+
```
40+
41+
Let's create an RQL Filter Class for `Product` model. All you need is to inherit from `dj_rql.filter_cls.RQLFilterClass`, define `MODEL` property and add supported `FILTERS` for class
42+
43+
```
44+
from dj_rql.filter_cls import RQLFilterClass
45+
46+
47+
class ProductFilters(RQLFilterClass):
48+
MODEL = Product
49+
FILTERS = (
50+
'id',
51+
'name',
52+
)
53+
54+
```
55+
56+
Using simple strings in `FILTERS` property you can define what fields are available for filtering. In example above you allow filtering only by `id` and `name` filter
57+
58+
Add RQL Filter Class to DRF View
59+
--------------------------------
60+
61+
In your latest step you need to add `ProductFilters` class as a `rql_filter_class` property inside your View
62+
63+
```
64+
class ProductsViewSet(mixins.ListModelMixin, GenericViewSet):
65+
queryset = Product.objects.all()
66+
serializer_class = ProductSerializer
67+
rql_filter_class = ProductFilters
68+
```
69+
70+
And that's it! Now you are able to start your local server and try to filter using RQL syntax
71+
72+
```
73+
curl http://127.0.0.1:8080/api/v1/products?like(name,Unicorn*)|eq(name,LLC)
74+
```
75+
76+
For learning RQL Syntax use following links:
77+
78+
[RQL Reference][rql_reference]
79+
80+
[RQL for Web][rql_for_web]
81+
82+
For learning how to define more complex filters use [Filters Guide][filters_guide]
83+
84+
[rql_reference]: https://connect.cloudblue.com/community/api/rql/
85+
[rql_for_web]: https://www.sitepen.com/blog/resource-query-language-a-query-language-for-the-web-nosql/
86+
[filters_guide]: ./filters_guide.md

0 commit comments

Comments
 (0)