gpt4 book ai didi

python - Django REST Framework——从未调用过更新 View 方法

转载 作者:行者123 更新时间:2023-11-30 22:54:37 25 4
gpt4 key购买 nike

我正在使用 Django REST Framework 设计一个 API 端点,并希望它在以下端点接受 GET、PATCH 和 PUT 方法:

/department/<department_pk>/details/population/

Department 模型和 DepartmentPopulationDetail 模型之间存在 1:1 关系(DepartmentPopulationDetail 模型有一个名为 departmnet 的 FK)。 GET 方法应返回 DepartmentPopulationDetail 模型实例,PUT 和 PATCH 应允许编辑同一实例(无需提供 DepartmentPopulationDetail PK)。

我已经设法让端点返回所需的 GET 响应,并且不允许 DELETE 或 CREATE,但无法让它接受 PUT 或 PATCH 请求 - 事实上,更新方法从未被调用。如何让我的 View 接受 PUT 和 PATCH 请求并编辑适当的模型?

urls.py
...
url(r'^departments/(?P<department_pk>[0-9]+)/details/population',
include(department_urls.department_study_population_router.urls,
namespace='department_study_population')),
...


serializers.py

class DepartmentStudyPopulationSerializer(serializers.ModelSerializer):

class Meta:
model = DepartmentStudyPopulation
fields = ('pk', 'department', 'age', 'zip_code')
read_only_fields = ('pk', 'department')

views.py
class DepartmentStudyPopulationViewSet(viewsets.ModelViewSet):
queryset = DepartmentStudyPopulation.objects.all()
serializer_class = DepartmentStudyPopulationSerializer
http_method_names = ['get', 'put', 'patch']

def update(self, request, department_pk, **kwargs):
queryset = departmentStudyPopulation.objects.all()
study_population = get_object_or_404(queryset, department_pk=department_pk)
serializer = DepartmentStudyPopulationSerializer(
study_population, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

最佳答案

为什么 update()方法永远不会被调用

这是因为您错误地定义了网址。

这个网址

'departments/(?P<department_pk>[0-9]+)/details/population'

使用路由器生成以下网址。

'departments/(?P<department_pk>[0-9]+)/details/population/' # handle list views
'departments/(?P<department_pk>[0-9]+)/details/population/<pk>/' # handle detail views

所以,你的update()仅当以下网址有请求时才会调用该方法,其中 pk代表idDepartmentStudyPopulation对象。

'departments/(?P<department_pk>[0-9]+)/details/population/<pk>/'

如何处理updateretrieve然后请求所需的网址?

您需要进行一些更改才能处理 updateretrieve在以下所需的 url 端点上发出请求。

/department/<department_pk>/details/population/

首先,您应该使用 RetrieveUpdateAPIView 而不是ModelViewSet因为您只需要处理 detail路线。此通用 View 仅允许 GET , PUTPATCH方法。

其次,您将使用 department_pk 获取对象实例。 url kwarg 通过过滤department_pk字段而不是 pk DepartmentStudyPopulation领域目的。您应该只指定 lookup_fieldlookup_url_kwarg值为 department_pk在您看来,DRF 将处理实例检索。

最终代码:

views.py

class DepartmentStudyPopulationDetailView(generics.RetrieveUpdateAPIView):

queryset = DepartmentStudyPopulation.objects.all()
serializer_class = DepartmentStudyPopulationSerializer
lookup_field = 'department_pk'
lookup_url_kwarg = 'department_pk'

urls.py

url(r'^departments/(?P<department_pk>[0-9]+)/details/population', DepartmentStudyPopulationDetailView.as_view())

关于python - Django REST Framework——从未调用过更新 View 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37705233/

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