Python Django REST框架--从未调用更新视图方法
我正在使用Django REST Framework设计一个API端点,并希望它在以下端点接受GET、PATCH和PUT方法:Python Django REST框架--从未调用更新视图方法,python,django,rest,django-rest-framework,Python,Django,Rest,Django Rest Framework,我正在使用Django REST Framework设计一个API端点,并希望它在以下端点接受GET、PATCH和PUT方法: /department/<department_pk>/details/population/ 您将需要一个不同的URL来处理这些情况 Create意味着您的DepartmentStudyPopulation是新的,并且没有主键(它是在createion上生成的) 更新需要一个包含主键的url,以便Django知道要更新的对象 (显示URL的文件将有助于解
/department/<department_pk>/details/population/
您将需要一个不同的URL来处理这些情况 Create意味着您的DepartmentStudyPopulation是新的,并且没有主键(它是在createion上生成的) 更新需要一个包含主键的url,以便Django知道要更新的对象
(显示URL的文件将有助于解决此问题,因为我们不知道它们将路由到哪里) 您需要一个不同的URL来处理这些情况 Create意味着您的DepartmentStudyPopulation是新的,并且没有主键(它是在createion上生成的) 更新需要一个包含主键的url,以便Django知道要更新的对象
(显示URL的文件将有助于解决此问题,因为我们不知道它们将路由到哪里) 为什么从未调用
update()
方法
这是因为您错误地定义了URL
此url
'departments/(?P<department_pk>[0-9]+)/details/population'
首先,您应该使用而不是ModelViewSet
,因为您只需要处理detail
路由。此通用视图仅允许使用GET
、PUT
和PATCH
方法
其次,通过过滤department\u pk
字段而不是DepartmentStudyPopulation
对象的pk
字段,使用department\u pk
url kwarg获取对象实例。您只需在视图中指定值为department\u pk
的lookup\u字段
和lookup\u url\u kwarg
,DRF将处理实例检索
最终代码:
视图.py
class DepartmentStudyPopulationDetailView(generics.RetrieveUpdateAPIView):
queryset = DepartmentStudyPopulation.objects.all()
serializer_class = DepartmentStudyPopulationSerializer
lookup_field = 'department_pk'
lookup_url_kwarg = 'department_pk'
url(r'^departments/(?P<department_pk>[0-9]+)/details/population', DepartmentStudyPopulationDetailView.as_view())
url.py
class DepartmentStudyPopulationDetailView(generics.RetrieveUpdateAPIView):
queryset = DepartmentStudyPopulation.objects.all()
serializer_class = DepartmentStudyPopulationSerializer
lookup_field = 'department_pk'
lookup_url_kwarg = 'department_pk'
url(r'^departments/(?P<department_pk>[0-9]+)/details/population', DepartmentStudyPopulationDetailView.as_view())
url(r'^departments/(?P[0-9]+)/details/population',DepartmentStudyPopulationDetailView.as_view())
为什么从未调用update()
方法
这是因为您错误地定义了URL
此url
'departments/(?P<department_pk>[0-9]+)/details/population'
首先,您应该使用而不是ModelViewSet
,因为您只需要处理detail
路由。此通用视图仅允许使用GET
、PUT
和PATCH
方法
其次,通过过滤department\u pk
字段而不是DepartmentStudyPopulation
对象的pk
字段,使用department\u pk
url kwarg获取对象实例。您只需在视图中指定值为department\u pk
的lookup\u字段
和lookup\u url\u kwarg
,DRF将处理实例检索
最终代码:
视图.py
class DepartmentStudyPopulationDetailView(generics.RetrieveUpdateAPIView):
queryset = DepartmentStudyPopulation.objects.all()
serializer_class = DepartmentStudyPopulationSerializer
lookup_field = 'department_pk'
lookup_url_kwarg = 'department_pk'
url(r'^departments/(?P<department_pk>[0-9]+)/details/population', DepartmentStudyPopulationDetailView.as_view())
url.py
class DepartmentStudyPopulationDetailView(generics.RetrieveUpdateAPIView):
queryset = DepartmentStudyPopulation.objects.all()
serializer_class = DepartmentStudyPopulationSerializer
lookup_field = 'department_pk'
lookup_url_kwarg = 'department_pk'
url(r'^departments/(?P<department_pk>[0-9]+)/details/population', DepartmentStudyPopulationDetailView.as_view())
url(r'^departments/(?P[0-9]+)/details/population',DepartmentStudyPopulationDetailView.as_view())
你也可以发布你的路由器吗?你也可以发布你的路由器吗?