简体   繁体   English

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

[英]How i can reformat this code to follow DRY principle

my views.py我的观点.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,)

my urls.py我的 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)

In my views i repeat a lot of identic stuff for different models(products, storage, productsign) therefore i disturb DRY principle, and i need to cut back some code.在我看来,我为不同的模型(产品、存储、产品设计)重复了很多相同的东西,因此我扰乱了 DRY 原则,我需要减少一些代码。 Maybe i can somehow habitate from previous classes or use something else.也许我可以以某种方式从以前的课程中居住或使用其他东西。 In my views i repeat a lot of identic stuff for different models(products, storage, productsign) therefore i disturb DRY principle, and i need to cut back some code.在我看来,我为不同的模型(产品、存储、产品设计)重复了很多相同的东西,因此我扰乱了 DRY 原则,我需要减少一些代码。 Maybe i can somehow habitate from previous classes or use something else.也许我可以以某种方式从以前的课程中居住或使用其他东西。

I think you can use the ModelViewSet to combine the above three classes into one.我认为您可以使用ModelViewSet将上述三个类合二为一。 The ModelViewSet class has create , retrieve , update , partial_update , destroy and list methods for all of the GET , POST , PATCH , PUT , and DELETE APIs. ModelViewSet class 具有所有GETPOSTPATCHPUTDELETE API 的createretrieveupdatepartial_updatedestroylist方法。

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

And in urls.py, you could just set one endpoint for all CRUD requests.在 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)

Then, for list() and create() methods (GET, POST), you can use api/v1/products , and retrieve() , update() , partial_update() , and delete() methods (GET, PUT, PATCH, UPDATE), you can use api/v1/products/<int:pk> .然后,对于list()create()方法(GET、POST),您可以使用api/v1/productsretrieve()update()partial_update()delete()方法(GET、PUT、PATCH ,更新),您可以使用api/v1/products/<int:pk>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM