Skip to content

Commit

Permalink
Add Quickstart Guide in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Lonkin committed Jul 26, 2020
1 parent 2e9518a commit ffb772f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Currently supported operators
0. Select (select)


Quickstart
==========
Try to enrich your API filtering opportunities learning [Quickstart Guide][quickstart]


Example
=======
```python
Expand Down Expand Up @@ -207,3 +212,5 @@ Tests reports are generated in `tests/reports`.

To generate HTML coverage reports use:
`--cov-report html:tests/reports/cov_html`

[quickstart]: ./docs/quickstart.md
86 changes: 86 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Quickstart
==========

We're going to create a simple API and configure filters to support RQL syntax.


Installation
------------
Install `django-rql` library in your existing or new Django and Django REST Framework project using command

```
pip install django-rql
```

Configuring Django settings
---------------------------

Setup default `filter_backends` in your Django settings file

```
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['dj_rql.drf.RQLFilterBackend']
}
```

Now your APIs are supporting RQL syntax for query strings. Let's write some filters

Write your first RQL Filter Class
---------------------------------

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

```
from django.db import models
class Product(models.Model):
name = models.CharField()
```

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

```
from dj_rql.filter_cls import RQLFilterClass
class ProductFilters(RQLFilterClass):
MODEL = Product
FILTERS = (
'id',
'name',
)
```

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

Add RQL Filter Class to DRF View
--------------------------------

In your latest step you need to add `ProductFilters` class as a `rql_filter_class` property inside your View

```
class ProductsViewSet(mixins.ListModelMixin, GenericViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
rql_filter_class = ProductFilters
```

And that's it! Now you are able to start your local server and try to filter using RQL syntax

```
curl http://127.0.0.1:8080/api/v1/products?like(name,Unicorn*)|eq(name,LLC)
```

For learning RQL Syntax use following links:

[RQL Reference][rql_reference]

[RQL for Web][rql_for_web]

For learning how to define more complex filters use [Filters Guide][filters_guide]

[rql_reference]: https://connect.cloudblue.com/community/api/rql/
[rql_for_web]: https://www.sitepen.com/blog/resource-query-language-a-query-language-for-the-web-nosql/
[filters_guide]: ./filters_guide.md

0 comments on commit ffb772f

Please sign in to comment.