我如何重新格式化此代码以遵循 DRY 原则



my views.py

from rest_framework import generics
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.filters import SearchFilter, OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.permissions import IsAdminUser
from .serializers import *
from .permissions import IsAdminOrReadOnly
from .filters import *

class ProductsAPIList(generics.ListCreateAPIView):
queryset = Products.objects.all()
serializer_class = ProductsSerializer
pagination_class = LimitOffsetPagination
permission_classes = (IsAdminOrReadOnly,)
filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
search_fields = ('title', 'category__name')
filter_fields = ('category',)
ordering_fields = ('price',)
filterset_class = ProductsFilter

class ProductsAPIUpdate(generics.RetrieveUpdateAPIView):
queryset = Products.objects.all()
serializer_class = ProductsSerializer
permission_classes = (IsAdminOrReadOnly, )

class ProductsAPIRemove(generics.RetrieveDestroyAPIView):
queryset = Products.objects.all()
serializer_class = ProductsSerializer
permission_classes = (IsAdminOrReadOnly, )

class StorageAPIList(generics.ListCreateAPIView):
queryset = ProductsStorage.objects.all()
serializer_class = ProductsStorageSerializer
permission_classes = (IsAdminUser,)

class StorageAPIUpdate(generics.RetrieveUpdateAPIView):
queryset = ProductsStorage.objects.all()
serializer_class = ProductsStorageSerializer
permission_classes = (IsAdminUser,)

class StorageAPIRemove(generics.RetrieveDestroyAPIView):
queryset = ProductsStorage.objects.all()
serializer_class = ProductsStorageSerializer
permission_classes = (IsAdminUser,)

class ProductSignAPIList(generics.ListCreateAPIView):
queryset = ProductSign.objects.all()
serializer_class = ProductSignSerializer
permission_classes = (IsAdminUser,)

class ProductSignAPIUpdate(generics.RetrieveUpdateAPIView):
queryset = ProductSign
serializer_class = ProductSignSerializer
permission_classes = (IsAdminUser,)

class ProductSignAPIRemove(generics.RetrieveDestroyAPIView):
queryset = ProductsStorage.objects.all()
serializer_class = ProductSignSerializer
permission_classes = (IsAdminUser,)

我的 urls.py

from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView
from InternetShop import settings
from InternetShopApp.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/products/', ProductsAPIList.as_view()),
path('api/v1/products/<int:pk>/', ProductsAPIUpdate.as_view()),
path('api/v1/productsremove/<int:pk>/', ProductsAPIRemove.as_view()),
path('api/v1/storage/', StorageAPIList.as_view()),
path('api/v1/storage/<int:pk>/', StorageAPIList.as_view()),
path('api/v1/storageremove/<int:pk>/', StorageAPIList.as_view()),
path('api/v1/productsign/', ProductSignAPIList.as_view()),
path('api/v1/productsign/<int:pk>/', ProductSignAPIUpdate.as_view()),
path('api/v1/productsignremove/<int:pk>/', ProductSignAPIRemove.as_view()),
path('api/v1/login/', include('djoser.urls')),
path('api/v1/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/v1/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('api/v1/token/verify/', TokenVerifyView.as_view(), name='token_verify'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

在我看来,我为不同的模型(产品、存储、产品标志)重复了很多相同的东西,因此我扰乱了 DRY 原则,我需要减少一些代码。也许我可以以某种方式从以前的课程中居住或使用其他东西。 在我看来,我为不同的模型(产品、存储、产品标志)重复了很多相同的东西,因此我扰乱了 DRY 原则,我需要减少一些代码。也许我可以以某种方式从以前的课程中居住或使用其他东西。

我认为您可以使用ModelViewSet将上述三个类合并为一个。ModelViewSet类有createretrieveupdatepartial_updatedestroylist方法,用于所有GETPOSTPATCHPUTDELETEAPI。

from rest_framework import viewsets
class ProductsViewSet(viewsets.ModelViewSet):
queryset = Products.objects.all()
serializer_class = ProductsSerializer
pagination_class = LimitOffsetPagination
permission_classes = (IsAdminOrReadOnly,)
filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
search_fields = ('title', 'category__name')
filter_fields = ('category',)
ordering_fields = ('price',)
filterset_class = ProductsFilter

在 urls.py 中,您可以为所有 CRUD 请求设置一个端点。

...
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/products', ProductsViewSet, basename="product"),
# path('api/v1/products/', ProductsAPIList.as_view()),
# path('api/v1/products/<int:pk>/', ProductsAPIUpdate.as_view()),
# path('api/v1/productsremove/<int:pk>/', ProductsAPIRemove.as_view()),
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

然后,对于list()create()方法(GET,POST),您可以使用api/v1/products,以及retrieve()update()partial_update()delete()方法(GET,PUT,PATCH,UPDATE),您可以使用api/v1/products/<int:pk>

相关内容

  • 没有找到相关文章

最新更新