parent
e275862948
commit
c7b54fb2d5
8 changed files with 93 additions and 7 deletions
@ -0,0 +1,14 @@ |
||||
from rest_framework import generics |
||||
|
||||
from apps.goods.models import Goods |
||||
from apps.goods.serializers import GoodsSerializers |
||||
|
||||
|
||||
class GoodsGenerics(generics.ListCreateAPIView): |
||||
queryset = Goods.objects.all() |
||||
serializer_class = GoodsSerializers |
||||
|
||||
|
||||
class GoodsDetailGenerics(generics.RetrieveUpdateDestroyAPIView): |
||||
queryset = Goods.objects.all() |
||||
serializer_class = GoodsSerializers |
@ -0,0 +1,29 @@ |
||||
from rest_framework import viewsets |
||||
from rest_framework import mixins |
||||
from django_filters.rest_framework import DjangoFilterBackend |
||||
from rest_framework import filters |
||||
|
||||
from apps.goods.models import Goods |
||||
from apps.goods.serializers import GoodsSerializers |
||||
from common.mypagination import MyPagination |
||||
from common.myfilter import MyFilter |
||||
|
||||
|
||||
class GoodsViewSet(mixins.ListModelMixin, |
||||
mixins.RetrieveModelMixin, |
||||
mixins.CreateModelMixin, |
||||
mixins.UpdateModelMixin, |
||||
mixins.DestroyModelMixin, |
||||
viewsets.GenericViewSet): |
||||
queryset = Goods.objects.all() |
||||
serializer_class = GoodsSerializers |
||||
# 分页 |
||||
# pagination_class = MyPagination #针对viewset的分页设置,也可以走全局 |
||||
# 筛选 |
||||
filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter) |
||||
# filter_backends = (DjangoFilterBackend,) |
||||
filterset_class = MyFilter |
||||
# 搜索 |
||||
search_fields = ('name', 'price') |
||||
# 排序 |
||||
ordering_fields = ('id', 'name', 'price') |
@ -0,0 +1,13 @@ |
||||
import django_filters |
||||
from django_filters import rest_framework as filters |
||||
from apps.goods.models import Goods |
||||
|
||||
|
||||
class MyFilter(filters.FilterSet): |
||||
name = filters.CharFilter(field_name='name', lookup_expr='icontains') |
||||
max_price = filters.NumberFilter(field_name='price', lookup_expr='lte', label='最高价格') |
||||
min_price = filters.NumberFilter(field_name='price', lookup_expr='gte', label='最低价格') |
||||
|
||||
class Meta: |
||||
model = Goods |
||||
fields = ('name', 'max_price', 'min_price') |
@ -0,0 +1,8 @@ |
||||
from rest_framework.pagination import PageNumberPagination |
||||
|
||||
|
||||
class MyPagination(PageNumberPagination): |
||||
page_size = 2 |
||||
max_page_size = 5 |
||||
page_size_query_param = 'size' |
||||
page_query_param = 'page' |
Binary file not shown.
Loading…
Reference in new issue