Skip to content

Commit a12de94

Browse files
authored
Merge pull request #1 from django-json-api/master
Sync
2 parents 96c533b + a320536 commit a12de94

17 files changed

+684
-39
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ pip-delete-this-directory.txt
4747
*.sw*
4848
manage.py
4949
.DS_Store
50+
51+
# example database
52+
drf_example

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Christian Zosel <https://zosel.ch>
66
Greg Aker <[email protected]>
77
Jamie Bliss <[email protected]>
88
Jerel Unruh <[email protected]>
9+
Jonathan Senecal <[email protected]>
910
1011
1112
Matt Layman <https://www.mattlayman.com>

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[unreleased]
22

33
* Add testing configuration to `REST_FRAMEWORK` configuration as described in [DRF](https://www.django-rest-framework.org/api-guide/testing/#configuration)
4-
* Add sorting configuration to `REST_FRAMEWORK` as defined in [json api spec](http://jsonapi.org/format/#fetching-sorting)
54
* Add `HyperlinkedRelatedField` and `SerializerMethodHyperlinkedRelatedField`. See [usage docs](docs/usage.md#related-fields)
65
* Add related urls support. See [usage docs](docs/usage.md#related-urls)
7-
6+
* Replaced binary `drf_example` sqlite3 db with a [fixture](example/fixtures/drf_example.yaml). See [usage docs](docs/usage.md#running-the-example-app).
7+
* Add optional [jsonapi-style](http://jsonapi.org/format/) sort filter backend. See [usage docs](docs/usage.md#filter-backends)
8+
* For naming consistency, renamed new `JsonApi`-prefix pagination classes to `JSONAPI`-prefix.
9+
* Deprecates `JsonApiPageNumberPagination` and `JsonApiLimitOffsetPagination`
810

911
v2.5.0 - Released July 11, 2018
1012

README.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ override ``settings.REST_FRAMEWORK``
161161
'PAGE_SIZE': 10,
162162
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
163163
'DEFAULT_PAGINATION_CLASS':
164-
'rest_framework_json_api.pagination.JsonApiPageNumberPagination',
164+
'rest_framework_json_api.pagination.JSONAPIPageNumberPagination',
165165
'DEFAULT_PARSER_CLASSES': (
166166
'rest_framework_json_api.parsers.JSONParser',
167167
'rest_framework.parsers.FormParser',
@@ -173,9 +173,8 @@ override ``settings.REST_FRAMEWORK``
173173
),
174174
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
175175
'DEFAULT_FILTER_BACKENDS': (
176-
'rest_framework.filters.OrderingFilter',
176+
'rest_framework_json_api.backends.JSONAPIOrderingFilter',
177177
),
178-
'ORDERING_PARAM': 'sort',
179178
'TEST_REQUEST_RENDERER_CLASSES': (
180179
'rest_framework_json_api.renderers.JSONRenderer',
181180
),

docs/getting-started.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ From Source
6868

6969
## Running the example app
7070

71-
git clone https://github.com/django-json-api/django-rest-framework-json-api.git
72-
cd django-rest-framework-json-api
73-
python -m venv env
74-
source env/bin/activate
75-
pip install -r example/requirements.txt
71+
git clone https://github.com/django-json-api/django-rest-framework-json-api.git
72+
cd django-rest-framework-json-api
73+
python3 -m venv env
74+
source env/bin/activate
75+
pip install -r example/requirements.txt
7676
pip install -e .
77-
django-admin.py startproject example .
78-
python manage.py migrate
79-
python manage.py runserver
77+
django-admin migrate --settings=example.settings
78+
django-admin loaddata drf_example --settings=example.settings
79+
django-admin runserver --settings=example.settings
80+
8081

8182
Browse to http://localhost:8000
8283

docs/usage.md

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
# Usage
33

4-
The DJA package implements a custom renderer, parser, exception handler, and
4+
The DJA package implements a custom renderer, parser, exception handler, query filter backends, and
55
pagination. To get started enable the pieces in `settings.py` that you want to use.
66

7-
Many features of the JSON:API format standard have been implemented using Mixin classes in `serializers.py`.
7+
Many features of the [JSON:API](http://jsonapi.org/format) format standard have been implemented using
8+
Mixin classes in `serializers.py`.
89
The easiest way to make use of those features is to import ModelSerializer variants
910
from `rest_framework_json_api` instead of the usual `rest_framework`
1011

@@ -15,7 +16,7 @@ REST_FRAMEWORK = {
1516
'PAGE_SIZE': 10,
1617
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
1718
'DEFAULT_PAGINATION_CLASS':
18-
'rest_framework_json_api.pagination.JsonApiPageNumberPagination',
19+
'rest_framework_json_api.pagination.JSONAPIPageNumberPagination',
1920
'DEFAULT_PARSER_CLASSES': (
2021
'rest_framework_json_api.parsers.JSONParser',
2122
'rest_framework.parsers.FormParser',
@@ -32,9 +33,8 @@ REST_FRAMEWORK = {
3233
),
3334
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
3435
'DEFAULT_FILTER_BACKENDS': (
35-
'rest_framework.filters.OrderingFilter',
36+
'rest_framework_json_api.filters.JSONAPIOrderingFilter',
3637
),
37-
'ORDERING_PARAM': 'sort',
3838
'TEST_REQUEST_RENDERER_CLASSES': (
3939
'rest_framework_json_api.renderers.JSONRenderer',
4040
),
@@ -58,15 +58,15 @@ You can configure fixed values for the page size or limit -- or allow the client
5858
via query parameters.
5959

6060
Two pagination classes are available:
61-
- `JsonApiPageNumberPagination` breaks a response up into pages that start at a given page number with a given size
62-
(number of items per page). It can be configured with the following attributes:
61+
- `JSONAPIPageNumberPagination` breaks a response up into pages that start at a given page number
62+
with a given size (number of items per page). It can be configured with the following attributes:
6363
- `page_query_param` (default `page[number]`)
6464
- `page_size_query_param` (default `page[size]`) Set this to `None` if you don't want to allow the client
6565
to specify the size.
6666
- `max_page_size` (default `100`) enforces an upper bound on the `page_size_query_param`.
6767
Set it to `None` if you don't want to enforce an upper bound.
68-
- `JsonApiLimitOffsetPagination` breaks a response up into pages that start from an item's offset in the viewset for
69-
a given number of items (the limit).
68+
- `JSONAPILimitOffsetPagination` breaks a response up into pages that start from an item's offset
69+
in the viewset for a given number of items (the limit).
7070
It can be configured with the following attributes:
7171
- `offset_query_param` (default `page[offset]`).
7272
- `limit_query_param` (default `page[limit]`).
@@ -77,19 +77,62 @@ Two pagination classes are available:
7777
These examples show how to configure the parameters to use non-standard names and different limits:
7878

7979
```python
80-
from rest_framework_json_api.pagination import JsonApiPageNumberPagination, JsonApiLimitOffsetPagination
80+
from rest_framework_json_api.pagination import JSONAPIPageNumberPagination, JSONAPILimitOffsetPagination
8181

82-
class MyPagePagination(JsonApiPageNumberPagination):
82+
class MyPagePagination(JSONAPIPageNumberPagination):
8383
page_query_param = 'page_number'
8484
page_size_query_param = 'page_size'
8585
max_page_size = 1000
8686

87-
class MyLimitPagination(JsonApiLimitOffsetPagination):
87+
class MyLimitPagination(JSONAPILimitOffsetPagination):
8888
offset_query_param = 'offset'
8989
limit_query_param = 'limit'
9090
max_limit = None
9191
```
9292

93+
### Filter Backends
94+
95+
_This is the first of several anticipated JSON:API-specific filter backends._
96+
97+
#### `JSONAPIOrderingFilter`
98+
`JSONAPIOrderingFilter` implements the [JSON:API `sort`](http://jsonapi.org/format/#fetching-sorting) and uses
99+
DRF's [ordering filter](http://django-rest-framework.readthedocs.io/en/latest/api-guide/filtering/#orderingfilter).
100+
101+
Per the JSON:API specification, "If the server does not support sorting as specified in the query parameter `sort`,
102+
it **MUST** return `400 Bad Request`." For example, for `?sort=`abc,foo,def` where `foo` is a valid
103+
field name and the other two are not valid:
104+
```json
105+
{
106+
"errors": [
107+
{
108+
"detail": "invalid sort parameters: abc,def",
109+
"source": {
110+
"pointer": "/data"
111+
},
112+
"status": "400"
113+
}
114+
]
115+
}
116+
```
117+
118+
If you want to silently ignore bad sort fields, just use `rest_framework.filters.OrderingFilter` and set
119+
`ordering_param` to `sort`.
120+
121+
#### Configuring Filter Backends
122+
123+
You can configure the filter backends either by setting the `REST_FRAMEWORK['DEFAULT_FILTER_BACKENDS']` as shown
124+
in the [preceding](#configuration) example or individually add them as `.filter_backends` View attributes:
125+
126+
```python
127+
from rest_framework_json_api import filters
128+
129+
class MyViewset(ModelViewSet):
130+
queryset = MyModel.objects.all()
131+
serializer_class = MyModelSerializer
132+
filter_backends = (filters.JSONAPIOrderingFilter,)
133+
```
134+
135+
93136
### Performance Testing
94137

95138
If you are trying to see if your viewsets are configured properly to optimize performance,

drf_example

-79 KB
Binary file not shown.

0 commit comments

Comments
 (0)