更新后尝试将 django rest 框架中的更新方法覆盖 return 整个查询集
Attempting to override update method in django rest framework to return entire queryset after update
我正在尝试覆盖 django rest 框架中放置请求的更新方法。而不是 return 只更新更新的对象。我希望它 return 包括更新对象在内的整个查询集。对于我正在处理的用例,它更容易。
从头开始。
我正在使用 Django Rest Frameworks 泛型。
class SearchCityDetail(RetrieveUpdateDestroyAPIView):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
我重写了 classes PUT
方法并从我的自定义混合中继承。
class SearchCityDetail(RetrieveUpdateDestroyAPIView, UpdateReturnAll):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
def put(self, request, *args, **kwargs):
return self.updatereturnall(self,request, *args, **kwargs)
自定义 mixin 看起来像这样(我自定义添加的代码与正常更新代码不同,上面有注释#Custom Code:
from rest_framework import status
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.mixins import UpdateModelMixin
"""
Update a model instance and return all.
"""
#Custom Code
class UpdateReturnAll(UpdateModelMixin):
#custom name normally just update
def updatereturnall(self, request, model, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
# If 'prefetch_related' has been applied to a queryset, we need to
# forcibly invalidate the prefetch cache on the instance.
instance._prefetched_objects_cache = {}
#all objects and all serializer is custom code
allobjects = self.get_queryset(self)
allserializer = self.get_serializer(allobjects, many=True)
return Response(allserializer.data)
def perform_update(self, serializer):
serializer.save()
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
self.get_queryset
和 self.get_serializer
是 GenericAPIView
中定义的函数,RetrieveUpdateDestroyAPIView
继承自该函数。由于我将 UpdateReturnAll
继承到 SearchCityDetail
class 中,这两种方法应该可用于 UpdateReturnALL
这是我的理解。
我目前收到错误和状态代码 500
错误是:AttributeError: 'SearchCityDetail' object has no attribute 'data'
我做错了什么?
应该是:
class SearchCityDetail(RetrieveUpdateDestroyAPIView, UpdateReturnAll):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
def put(self, request, *args, **kwargs):
return self.updatereturnall(request, *args, **kwargs)
而不是return self.updatereturnall(self,request, *args, **kwargs)
。
调用 self.updatereturnall
方法时不需要显式传递 self
参数,Python 为您完成。
我正在尝试覆盖 django rest 框架中放置请求的更新方法。而不是 return 只更新更新的对象。我希望它 return 包括更新对象在内的整个查询集。对于我正在处理的用例,它更容易。
从头开始。
我正在使用 Django Rest Frameworks 泛型。
class SearchCityDetail(RetrieveUpdateDestroyAPIView):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
我重写了 classes PUT
方法并从我的自定义混合中继承。
class SearchCityDetail(RetrieveUpdateDestroyAPIView, UpdateReturnAll):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
def put(self, request, *args, **kwargs):
return self.updatereturnall(self,request, *args, **kwargs)
自定义 mixin 看起来像这样(我自定义添加的代码与正常更新代码不同,上面有注释#Custom Code:
from rest_framework import status
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.mixins import UpdateModelMixin
"""
Update a model instance and return all.
"""
#Custom Code
class UpdateReturnAll(UpdateModelMixin):
#custom name normally just update
def updatereturnall(self, request, model, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
# If 'prefetch_related' has been applied to a queryset, we need to
# forcibly invalidate the prefetch cache on the instance.
instance._prefetched_objects_cache = {}
#all objects and all serializer is custom code
allobjects = self.get_queryset(self)
allserializer = self.get_serializer(allobjects, many=True)
return Response(allserializer.data)
def perform_update(self, serializer):
serializer.save()
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
self.get_queryset
和 self.get_serializer
是 GenericAPIView
中定义的函数,RetrieveUpdateDestroyAPIView
继承自该函数。由于我将 UpdateReturnAll
继承到 SearchCityDetail
class 中,这两种方法应该可用于 UpdateReturnALL
这是我的理解。
我目前收到错误和状态代码 500
错误是:AttributeError: 'SearchCityDetail' object has no attribute 'data'
我做错了什么?
应该是:
class SearchCityDetail(RetrieveUpdateDestroyAPIView, UpdateReturnAll):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
def put(self, request, *args, **kwargs):
return self.updatereturnall(request, *args, **kwargs)
而不是return self.updatereturnall(self,request, *args, **kwargs)
。
调用 self.updatereturnall
方法时不需要显式传递 self
参数,Python 为您完成。