2

The response from my application is as below, but I want to add another field next to previous in metadata, how to add IP to metadata

{
 "count": 100,
 "next": "http://127.0.0.1:8000/users/?page=2",
 "previous": null,
 "results": [
         {
          "first_name": "john",
          "last_name": "a",
          "mail": "[email protected]"
         }
  ]
}

I want my results to be:

    {
     "count": 100,
     "next": "http://127.0.0.1:8000/users/?page=2",
     "previous": null,
     "ip-adress":0.0.0(something)
     "results": [
             {
}
2
  • what did you mean add metadata without adding can you show example? Commented Apr 10, 2018 at 6:16
  • I have made some changes in the question @Bear Brown Commented Apr 10, 2018 at 6:23

1 Answer 1

1

by docs custom-pagination-styles you can try

class CustomPagination(pagination.PageNumberPagination):
    def ip_data(self):
        # YOU CUSTOM CODE HERE
        return '0.0.0.0'

    def get_paginated_response(self, data):

        return Response({
            'next': self.get_next_link(),
            'previous': self.get_previous_link()
            'count': self.page.paginator.count,
            'results': data,
            'ip-adress': self.ip_data()
            # ^^^^^^^^^^^^^^^^^^^^^^
        })

and in the settings update data for paginator

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'my_project.core.pagination.CustomPagination',
     #                      ^^^ CHANGE PATH TO YOUR PAGINATOR CLASS ^^^
    'PAGE_SIZE': 100
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you,and we can just include our pagination into views as pagination_class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.