KF pubsub
JsonFieldFilter and JsonFieldSearchFilter implementation with django and django rest framework
Installation
With requirements.txt
Add the following line in your requirements.txt file:
kfjsonfilter
With PIP
$ pip install kfjsonfilter
Getting Started
There are implemented two base filters: JsonFieldFilter and JsonFieldSearchFilter.
JsonFieldFilter
Allow create a data filter from a json object. Its neccesary to declare two elements in the django rest framework viewset: json_field and json_filters
| TYPE | DESCRIPTION | |
|---|---|---|
| json_field | string | Indicates the json field object form the model |
| json_filters | list of dicts | Indicates the field thats allow filter and lookup type for each type |
json_filters structure
Allows filter by any value from a json data. Each element must be included in a dictionary with to key: field and lookup.
json_filters = [{'field': <field_name>, 'lookup': <lookup>}, ...]
| DESCRIPTION | |
|---|---|
| field | field_name is the name of the element to filter included in json data |
| lookup | lookup is the filter type, icontains, iexact, in, etc... and date_range (to filter by a date range). to filter only a date by a date indicates only lte, gte, exact... |
JsonFieldSearchFilter
Allow create a data filter and search filter from a json object. Its neccesary to declare three elements in the django rest framework viewset: json_field and search_fields
| TYPE | DESCRIPTION | |
|---|---|---|
| json_field | string | Indicates the json field object form the model |
| json_filters | list of dicts | Indicates the field thats allow filter by a field and lookup type for each type |
| search_fields | list of dicts | Indicates the field thats allow search by a field and lookup type for each type |
json_filters and search_fields structure
Allows filter/search by any value from a json data. Each element must be included in a dictionary with to key: field and lookup.
json_filters = [{'field': <field_name>, 'lookup': <lookup>}, ...]
| DESCRIPTION | |
|---|---|
| field | field_name is the name of the element to filter included in json data |
| lookup | lookup is the filter type, icontains, iexact, in, etc... and date_range (to filter by a date range). to filter only a date by a date indicates only lte, gte, exact... |
Also, when lookup is 'date_range' field_name must finish with []
Example
Django Model
class MyModel(models.Model):
data = JSONField(default=dict)
For example:
data = {'creation_date': '2020-01-01', 'total': 3, 'name': 'Pedro Miguel'}
Django Rest Framework View
class MyModelViewSet(ListAPIView):
queryset = MyModel.objects.all()
filter_backends = [JsonFieldFilter, DjangoFilterBackend, OrderingFilter]
json_field = 'data'
json_filters = [{'field': 'creation_date', 'lookup': 'date_range'}, {'field': 'total', 'lookup': 'gte'}]
or
class MyModelViewSet(ListAPIView):
queryset = MyModel.objects.all()
filter_backends = [JsonFieldSearchFilter, DjangoFilterBackend, OrderingFilter]
json_field = 'data'
json_filters = [{'field': 'creation_date', 'lookup': 'lte'}, {'field': 'total', 'lookup': 'gte'}]
search_fields = [{'field': 'name', 'lookup': 'icontains'}]
Release files for kfjsonfilter 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kfjsonfilter-0.1.5.tar.gz | 3.4 kB | Details |
Release files / kfjsonfilter-0.1.5.tar.gz
| Download URL | kfjsonfilter-0.1.5.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba081e91407bdc92ac55e498841483e9f6efa77cd091a555d3d18cbb82f6650b
|
|
BLAKE2b-256 checksum How to use checksums |
3896d28ef94516b8b3a9cd50d5846321a83d566252f2120f03be3793658beacf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
|