我正在尝试进行分页并阅读此链接。
使用 API-View 在 Django-Rest-Framework 中分页 https://stackoverflow.com/questions/29071312/pagination-in-django-rest-framework-using-api-view
我需要有下一个、上一个 url 进行分页。问题是当我打电话给这个的时候
pagination_class = settings.DEFAULT_PAGINATION_CLASS
It say
“设置”对象没有属性“DEFAULT_PAGINATION_CLASS”
我需要导入什么?或安装?
模型.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)
序列化器.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)
设置.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',)
}