完成了viewset, 自定义筛选,搜索, 排序的学习

main
RogerWork 1 year ago
parent e275862948
commit c7b54fb2d5
  1. 16
      apps/goods/urls.py
  2. 14
      apps/goods/views_generics.py
  3. 8
      apps/goods/views_mixin.py
  4. 29
      apps/goods/views_viewset.py
  5. 13
      common/myfilter.py
  6. 8
      common/mypagination.py
  7. 12
      myshop_back/settings.py
  8. BIN
      requirements.txt

@ -16,7 +16,17 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from apps.goods import views, views_api, views_apiview, views_mixin
from apps.goods import views, views_api, views_apiview, views_mixin, views_generics, views_viewset
goods_list = views_viewset.GoodsViewSet.as_view({
'get': 'list',
'post': 'create',
})
goods_detail = views_viewset.GoodsViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'delete': 'destroy',
})
urlpatterns = [
# path('cate_index/', views.GoodCateView.as_view(), name='cate_index'),
@ -32,4 +42,8 @@ urlpatterns = [
path('goods_apiview/<int:id>/', views_apiview.GoodsAPIView.as_view()),
path('goods_mixin/', views_mixin.GoodsMixin.as_view()),
path('goods_mixin/<pk>/', views_mixin.GoodsDetailMixin.as_view()),
path('goods_generics/', views_generics.GoodsGenerics.as_view()),
path('goods_generics/<pk>/', views_generics.GoodsDetailGenerics.as_view()),
path('goods_viewset/', goods_list),
path('goods_viewset/<pk>/', goods_detail),
]

@ -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

@ -2,12 +2,12 @@ from rest_framework import mixins
from rest_framework import generics
from apps.goods.models import Goods
from apps.goods.serializers import GoodsSerializers
from apps.goods.serializers import GoodsSerializers, GoodsModelSerializer
class GoodsMixin(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
queryset = Goods.objects.all().order_by('-id')[:3]
serializer_class = GoodsModelSerializer
# GoodsSerializers(queryset, many=True)
@ -21,7 +21,7 @@ class GoodsMixin(mixins.ListModelMixin, mixins.CreateModelMixin, generics.Generi
class GoodsDetailMixin(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin,
generics.GenericAPIView):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
serializer_class = GoodsModelSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

@ -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'

@ -11,9 +11,10 @@ https://docs.djangoproject.com/en/4.2/ref/settings/
"""
from pathlib import Path
from rest_framework.renderers import JSONRenderer
import sys, os
import rest_framework.filters
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
@ -45,6 +46,7 @@ INSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
'rest_framework',
'django_filters'
]
MIDDLEWARE = [
@ -146,9 +148,15 @@ auth.User.groups: (fields.E304) Reverse accessor 'Group.user_set' for 'auth.User
AUTH_USER_MODEL = 'user.Myuser'
REST_FRAMEWORK = {
# 设置全局渲染模板
'DEFAULT_RENDERER_CLASSES': (
# 'rest_framework.renderers.JSONRenderer',
'apps.goods.drf_customer_renderer.CustomRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
),
# 设置全局分页设置
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 5,
# 设置筛选模板
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
}

Binary file not shown.
Loading…
Cancel
Save