gpt4 book ai didi

Django Rest框架使用泛型查看组织

转载 作者:行者123 更新时间:2023-12-02 14:39:10 27 4
gpt4 key购买 nike

在这里迈出我在 django 中的第一步,构建一个简单的 crud api,其中我有两个实体:设备和属性。

我的项目urls.py看起来像这样:

from django.contrib import admin
from django.urls import include, path
from devices.views import index

urlpatterns = [
path('', index, name='index'),
path('admin/', admin.site.urls),
path('api/', include('devices.urls')),
]

和我的应用程序urls.py如下:

from django.urls import path
from . import views

urlpatterns = [
path('device/create/', views.DeviceCreate.as_view()),
path('device/update/<int:pk>/', views.DeviceUpdate.as_view()),
path('device/delete/<int:pk>/', views.DeviceDelete.as_view()),
path('device/', views.DeviceList.as_view()),
path('device/<int:pk>/', views.DeviceDetail.as_view()),

path('property/create/', views.PropertyCreate.as_view()),
path('property/update/<int:pk>/', views.PropertyUpdate.as_view()),
path('property/delete/<int:pk>/', views.PropertyDelete.as_view()),
path('property/', views.PropertyList.as_view()),
path('property/<int:pk>/', views.PropertyDetail.as_view()),
]

这有点重复,但没关系,真正的问题是我的 View 文件重复得多:

from rest_framework import generics

from .models import Device, Property
from .serializers import DeviceSerializer, PropertySerializer

from django.shortcuts import render

def index(request):
return render(request, 'index.html')

class DeviceCreate(generics.CreateAPIView):
queryset = Device.objects.all()
serializer_class = DeviceSerializer

class DeviceList(generics.ListAPIView):
queryset = Device.objects.all()
serializer_class = DeviceSerializer

class DeviceUpdate(generics.UpdateAPIView):
queryset = Device.objects.all()
serializer_class = DeviceSerializer

class DeviceDelete(generics.DestroyAPIView):
queryset = Device.objects.all()
serializer_class = DeviceSerializer

class DeviceDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Device.objects.all()
serializer_class = DeviceSerializer

class PropertyCreate(generics.CreateAPIView):
queryset = Property.objects.all()
serializer_class = PropertySerializer

class PropertyList(generics.ListAPIView):
queryset = Property.objects.all()
serializer_class = PropertySerializer

class PropertyUpdate(generics.UpdateAPIView):
queryset = Property.objects.all()
serializer_class = PropertySerializer

class PropertyDelete(generics.DestroyAPIView):
queryset = Property.objects.all()
serializer_class = PropertySerializer

class PropertyDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Property.objects.all()
serializer_class = PropertySerializer

我查看了网络示例,这似乎是一种有效的方法(加上我的应用程序正在运行),但是有没有办法改进我的 views.py 文件以获得相同的结果更少的代码?

最佳答案

您可以将一个 ModelViewSet 用于您的 PropertySerializer,并为您的 DeviceSerializer 使用一个

ModelViewSet提供默认的createretrieveupdatepartial_updatedestroy列出 操作。

您的 View 将如下所示:

class DeviceViewSet(ModelViewSet):
serializer_class = DeviceSerializer

def get_queryset(self):
return Device.objects.all()


class PropertyViewSet(ModelViewSet):
serializer_class = PropertySerializer

def get_queryset(self):
return Property.objects.all()

然后,您将在 urls.py 中向路由器注册您的 View ,如下所示:

from . import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'devices', views.DeviceViewSet, base_name='device')
router.register(r'properties', views.PropertyViewSet, base_name='property')
urlpatterns = router.urls

请注意,您的序列化器类也必须是 ModelSerializer:

class DeviceSerializer(ModelSerializer):
class Meta:
model = Device
fields = ('foo', 'bar')


class PropertySerializer(ModelSerializer):
class Meta:
model = Device
fields = ('foo', 'bar')

您可以在rest framework docs中阅读更多相关信息。

关于Django Rest框架使用泛型查看组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51962698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com