Django REST framwork 自带分页器,一种是在settings.py里配置,另外一种是在views.py
serializers.py
class GoodSerializer(serializers.Serializer):
name = serializers.CharField(required=True,max_length=100)
click_num = serializers.IntegerField(default=0)
def create(self, validated_data):
return Goods.objects.create(**validated_data)
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = GoodsCategory
fields = '__all__'
# category 是Goods的外键,如果想把外键表的信息给展示出来,需要将外键表也给序列化,
#然后用外键这个字段来实例化外键表
class GoodsSerializer(serializers.ModelSerializer):
category = CategorySerializer()
class Meta:
model = Goods
# fields = ('name', 'click_num', 'market_price', 'add_time')
fields = '__all__' #全部
views.py
from rest_framework.pagination import PageNumberPagination
from goods.serializer import GoodsSerializer
from users.models import Goods
# APIView
class GoodsListView(APIView):
def get(self, request, format=None):
goods = Goods.objects.all()[:10]
goods_serializer = GoodsSerializer(goods, many=True)
return Response(goods_serializer.data)
def post(self,request):
serializer = GoodSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data,status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# 分页
class GoodsPagination(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
page_query_param = 'p'
max_page_size = 100
class GoodsListView(generics.ListAPIView):
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
pagination_class = GoodsPagination
过滤
https://github.com/philipn/django-rest-framework-filters#filterset-usage
filters.py
import django_filters
from django.db.models import Q
from users.models import Goods
class GoodsFilter(django_filters.rest_framework.FilterSet):
'''
商品的过滤类
'''
# 取出最大值和最小值
price_min = django_filters.NumberFilter(name='shop_price', lookup_expr='gte')
price_max = django_filters.NumberFilter(name='shop_price', lookup_expr='lte')
top_category = django_filters.NumberFilter(method='top_category_filter')
# 参数是迷人传递进来的,需要手动将参数写入
# 查找第一类别的所有商品
def top_category_filter(self, queryset, name, value):
return queryset.filter(Q(category_id=value)|Q(category__parent_category_id=value)|Q(category__parent_category__parent_category_id=value))
# 模糊查询,类似MySQL里的like
name = django_filters.CharFilter(name='name', lookup_expr='icontains')
class Meta:
model = Goods
fields = ['price_min', 'price_max', 'name']
views.py
分页、搜索、过滤、排序
rom django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status, mixins, filters
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import generics
from rest_framework import viewsets
# class GoodsListViewSet(mixins.ListModelMixin,viewsets.GenericViewSet):
# '''
# 商品列表
# '''
# serializer_class = GoodsSerializer
# pagination_class = GoodsPagination
#
# # 过滤需要重写get_queryset
# def get_queryset(self):
# queryset = Goods.objects.all()
# price_min = self.request.query_params.get('price_min',0)
# if price_min:
# queryset = queryset.filter(shop_price__gt=int(price_min))
# return queryset
class GoodListViewSet(mixins.ListModelMixin,viewsets.GenericViewSet):
'''
商品列表页
'''
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
pagination_class = GoodsPagination
filter_backends = (DjangoFilterBackend,filters.SearchFilter,filters.OrderingFilter)
filter_class = GoodsFilter
# 前端请求的时候参数是?search= ...
search_fields = ('=name', 'goods_brief', 'goods_desc')
# 前端请求的时候参数是?order ing= ...
ordering_fields = ('sold_num', 'add_time')