More python blocks

This commit is contained in:
Steve Lacey 2015-02-27 16:35:21 +00:00
parent 29c8146c00
commit 3dc83fb458

View File

@ -21,9 +21,11 @@ Pagination is only performed automatically if you're using the generic views or
The default pagination style may be set globally, using the `DEFAULT_PAGINATION_CLASS` settings key. For example, to use the built-in limit/offset pagination, you would do: The default pagination style may be set globally, using the `DEFAULT_PAGINATION_CLASS` settings key. For example, to use the built-in limit/offset pagination, you would do:
REST_FRAMEWORK = { ```py
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination' REST_FRAMEWORK = {
} 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination'
}
```
You can also set the pagination class on an individual view by using the `pagination_class` attribute. Typically you'll want to use the same pagination style throughout your API, although you might want to vary individual aspects of the pagination, such as default or maximum page size, on a per-view basis. You can also set the pagination class on an individual view by using the `pagination_class` attribute. Typically you'll want to use the same pagination style throughout your API, although you might want to vary individual aspects of the pagination, such as default or maximum page size, on a per-view basis.
@ -31,28 +33,34 @@ You can also set the pagination class on an individual view by using the `pagina
If you want to modify particular aspects of the pagination style, you'll want to override one of the pagination classes, and set the attributes that you want to change. If you want to modify particular aspects of the pagination style, you'll want to override one of the pagination classes, and set the attributes that you want to change.
class LargeResultsSetPagination(PageNumberPagination): ```py
paginate_by = 1000 class LargeResultsSetPagination(PageNumberPagination):
paginate_by_param = 'page_size' paginate_by = 1000
max_paginate_by = 10000 paginate_by_param = 'page_size'
max_paginate_by = 10000
class StandardResultsSetPagination(PageNumberPagination): class StandardResultsSetPagination(PageNumberPagination):
paginate_by = 100 paginate_by = 100
paginate_by_param = 'page_size' paginate_by_param = 'page_size'
max_paginate_by = 1000 max_paginate_by = 1000
```
You can then apply your new style to a view using the `.pagination_class` attribute: You can then apply your new style to a view using the `.pagination_class` attribute:
class BillingRecordsView(generics.ListAPIView): ```py
queryset = Billing.objects.all() class BillingRecordsView(generics.ListAPIView):
serializer = BillingRecordsSerializer queryset = Billing.objects.all()
pagination_class = LargeResultsSetPagination serializer = BillingRecordsSerializer
pagination_class = LargeResultsSetPagination
```
Or apply the style globally, using the `DEFAULT_PAGINATION_CLASS` settings key. For example: Or apply the style globally, using the `DEFAULT_PAGINATION_CLASS` settings key. For example:
REST_FRAMEWORK = { ```py
'DEFAULT_PAGINATION_CLASS': 'apps.core.pagination.StandardResultsSetPagination' REST_FRAMEWORK = {
} 'DEFAULT_PAGINATION_CLASS': 'apps.core.pagination.StandardResultsSetPagination'
}
```
--- ---
@ -128,10 +136,12 @@ class LinkHeaderPagination(pagination.PageNumberPagination):
To have your custom pagination class be used by default, use the `DEFAULT_PAGINATION_CLASS` setting: To have your custom pagination class be used by default, use the `DEFAULT_PAGINATION_CLASS` setting:
REST_FRAMEWORK = { ```py
'DEFAULT_PAGINATION_CLASS': 'my_project.apps.core.pagination.LinkHeaderPagination', REST_FRAMEWORK = {
'PAGINATE_BY': 10 'DEFAULT_PAGINATION_CLASS': 'my_project.apps.core.pagination.LinkHeaderPagination',
} 'PAGINATE_BY': 10
}
```
API responses for list endpoints will now include a `Link` header, instead of including the pagination links as part of the body of the response, for example: API responses for list endpoints will now include a `Link` header, instead of including the pagination links as part of the body of the response, for example: