Json Data Filter for django and django rest framework
Project description
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'}]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file kfjsonfilter-0.1.5.tar.gz
.
File metadata
- Download URL: kfjsonfilter-0.1.5.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba081e91407bdc92ac55e498841483e9f6efa77cd091a555d3d18cbb82f6650b |
|
MD5 | 4f7752ffc0a62d637b8e20d2875e7474 |
|
BLAKE2b-256 | 3896d28ef94516b8b3a9cd50d5846321a83d566252f2120f03be3793658beacf |