Skip to content
This repository was archived by the owner on May 2, 2023. It is now read-only.

Format results client side

Maigret Aurélien edited this page Jun 27, 2017 · 5 revisions

Pagination

The results of type list can sometimes have a lot of data. Therefore it is necessary to split the data into multiple pages.

You can specify 2 parameters to manage the pagination of a response:

  • page: the current page (default 1)
  • limit: the limit of elements per page (default 25)
$> curl -X GET http://127.0.0.1:8000/api/1.0/project?limit=3
{
    "count_results": 3,
    "current_page": 1,
    "format": {
        "id": "id",
        "name": "name",
        "owner": {
            "email": "owner__email",
            "id": "owner__id"
        }
    },
    "limit": 3,
    "number_pages": 2,
    "results": [
        {
            "id": 1,
            "name": "Skies",
            "owner": {
                "email": "[email protected]",
                "id": 2
            }
        },
        {
            "id": 2,
            "name": "ComeOn",
            "owner": {
                "email": "[email protected]",
                "id": 1
            }
        },
        {
            "id": 3,
            "name": "IE-Android",
            "owner": {
                "email": "[email protected]",
                "id": 3
            }
        }
    ],
    "total_results": 4
}
$> curl -X GET "http://127.0.0.1:8000/api/1.0/project?limit=3&page=2"
{
    "count_results": 1,
    "current_page": 2,
    "format": {
        "id": "id",
        "name": "name",
        "owner": {
            "email": "owner__email",
            "id": "owner__id"
        }
    },
    "limit": 3,
    "number_pages": 2,
    "results": [
        {
            "id": 4,
            "name": "ProjectEIP",
            "owner": {
                "email": "[email protected]",
                "id": 4
            }
        }
    ],
    "total_results": 4
}

Order

You can also sort the results through a field:

  • order: The name of the field
  • sort: The sort order (asc or desc, default asc)

Example:

$> curl -X GET "http://127.0.0.1:8000/api/1.0/project?limit=3&order=id&sort=desc"
...

Filter

Finally, you could filter the results according to a field. To do so, specify the GET parameter 'filter' with the field of your choice and one of the following field lookups:

  • exact
  • iexact
  • contains
  • icontains
  • gt
  • gte
  • lt
  • lte
  • startswith
  • istartswith
  • endswith
  • iendswith
  • isnull
  • regex
  • iregex

You could see the behavior of each of these filters in the Django documentation.

$> curl -X GET "http://127.0.0.1:8000/api/1.0/project?filter=owner__id&gte=2&lt=4"
{
    "count_results": 2,
    "current_page": 1,
    "format": {
        "id": "id",
        "name": "name",
        "owner": {
            "email": "owner__email",
            "id": "owner__id"
        }
    },
    "limit": 25,
    "number_pages": 1,
    "results": [
        {
            "id": 1,
            "name": "Skies",
            "owner": {
                "email": "[email protected]",
                "id": 2
            }
        },
        {
            "id": 3,
            "name": "IE-Android",
            "owner": {
                "email": "[email protected]",
                "id": 3
            }
        }
    ],
    "total_results": 2
}
Clone this wiki locally