Khant Thu Linn
Khant Thu Linn

Reputation: 6133

Django pagination with next, previous, count

I am trying to do for pagination and reading through this link.

Pagination in Django-Rest-Framework using API-View

I need to have next, previous url for pagination. Problem is that when I call this one,

pagination_class = settings.DEFAULT_PAGINATION_CLASS

It say

'Settings' object has no attribute 'DEFAULT_PAGINATION_CLASS'

What do I need to import? or install ?

Models.py

class CarView(APIView):
permission_classes = ()

def get(self, request):
    """ Get all car """
    car = Car.objects.all()
    # paginator = PageNumberPagination()

    serializer = CarSerializer(car)
    serialized_car = CarSerializer(car, context={"request": request})
    # serializer = CarSerializer(car[0])
    return Response(serialized_car.data)

Serializers.py

class CarSerializer(serializers.ModelSerializer):

photo_url = serializers.SerializerMethodField('get_photo_url')

class Meta:
    model = Car
    fields = ('id','name','price', 'photo_url') 

def get_photo_url(self, car):
    request = self.context.get('request')
    photo_url = car.photo.url
    return request.build_absolute_uri(photo_url)

settings.py

REST_FRAMEWORK = {

'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100,

'DEFAULT_AUTHENTICATION_CLASSES':
    ('rest_framework.authentication.OAuth2Authentication',
     'rest_framework.authentication.SessionAuthentication'),

'DEFAULT_MODEL_SERIALIZER_CLASS':
    'rest_framework.serializers.ModelSerializer',

'DEFAULT_PERMISSION_CLASSES':
('rest_framework.permissions.IsAdminUser',)

}

Upvotes: 4

Views: 1559

Answers (1)

Forge
Forge

Reputation: 6834

Your settings file doesn't include a definition for DEFAULT_PAGINATION_CLASS, it does include a definition for REST_FRAMEWORK, and DEFAULT_PAGINATION_CLASS is part of it.

From Django Rest Framework documentation on accessing settings

Accessing settings

If you need to access the values of REST framework's API settings in your project, you should use the api_settings object. For example.

from rest_framework.settings import api_settings
print api_settings.DEFAULT_AUTHENTICATION_CLASSES

The api_settings object will check for any user-defined settings, and otherwise fall back to the default values. Any setting that uses string import paths to refer to a class will automatically import and return the referenced class, instead of the string literal.

You should be able to access api_settings.DEFAULT_PAGINATION_CLASS

Upvotes: 3

Related Questions