完成mixin代码

main
roger_home_pc 1 year ago
parent c1ae3201ea
commit e275862948
  1. 4
      apps/goods/urls.py
  2. 33
      apps/goods/views_mixin.py

@ -16,7 +16,7 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from apps.goods import views, views_api, views_apiview
from apps.goods import views, views_api, views_apiview, views_mixin
urlpatterns = [
# path('cate_index/', views.GoodCateView.as_view(), name='cate_index'),
@ -30,4 +30,6 @@ urlpatterns = [
path('goods_api_d/<int:id>/', views_api.goods_list),
path('goods_apiview/', views_apiview.GoodsAPIView.as_view()),
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()),
]

@ -0,0 +1,33 @@
from rest_framework import mixins
from rest_framework import generics
from apps.goods.models import Goods
from apps.goods.serializers import GoodsSerializers
class GoodsMixin(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
# GoodsSerializers(queryset, many=True)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
class GoodsDetailMixin(mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin,
generics.GenericAPIView):
queryset = Goods.objects.all()
serializer_class = GoodsSerializers
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
Loading…
Cancel
Save