You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+20-13Lines changed: 20 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Django RQL
8
8
RQL
9
9
---
10
10
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.
12
12
This is a query language fast and convenient database interaction. RQL was designed for use in URLs to request object-style data structures.
0. Support for custom fields, inherited at any depth from basic model fields, like CharField().
179
184
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.
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
0 commit comments