Django Rest Framework List API View 的筛选后端

huangapple go评论104阅读模式
英文:

Filter Backends Django Rest Framework List API View

问题

抱歉,我只回答不包含代码的请求。以下是您要翻译的内容:

Excuse me devs, I want to ask about Django generics List API View filter_backends, how can i return none / data not found when the param is wrong or empty?

  1. # My Django Views
  2. class FilterTablePVPlantByPVOwnerId(filters.FilterSet):
  3. id = filters.CharFilter(
  4. field_name='id', lookup_expr='exact')
  5. class Meta:
  6. model = TablePVPlant
  7. fields = ['id']
  8. class PlantListByClientView(generics.ListAPIView):
  9. queryset = TablePVPlant.objects.all()
  10. serializer_class = PlantListSerializer
  11. filter_backends = [DjangoFilterBackend]
  12. filterset_class = FilterTablePVPlantByPVOwnerId
  13. def list(self, request, *args, **kwargs):
  14. if self.request.query_params:
  15. response = super().list(request, *args, **kwargs)
  16. response.data = {'status': 'success',
  17. 'data': response.data, 'msg': 'done'}
  18. return response
英文:

Excuse me devs, I want to ask about Django generics List API View filter_backends, how can i return none / data not found when the param is wrong or empty?

  1. # My Django Views
  2. class FilterTablePVPlantByPVOwnerId(filters.FilterSet):
  3. id = filters.CharFilter(
  4. field_name='id', lookup_expr='exact')
  5. class Meta:
  6. model = TablePVPlant
  7. fields = ['id']
  8. class PlantListByClientView(generics.ListAPIView):
  9. queryset = TablePVPlant.objects.all()
  10. serializer_class = PlantListSerializer
  11. filter_backends = [DjangoFilterBackend]
  12. filterset_class = FilterTablePVPlantByPVOwnerId
  13. def list(self, request, *args, **kwargs):
  14. if self.request.query_params:
  15. response = super().list(request, *args, **kwargs)
  16. response.data = {'status': 'success',
  17. 'data': response.data, 'msg': 'done'}
  18. return response

答案1

得分: 1

在Django和Django Rest Framework中,这些类型的问题通过引发异常来处理:

  1. from rest_framework.exceptions import APIException
  2. class BadParameter(APIException):
  3. status_code = 400
  4. default_detail = '参数x错误或为空'
  5. default_code = 'bad_param'
  6. # ...
  7. class PlantListByClientView(generics.ListAPIView):
  8. # ...
  9. def list(self, request, *args, **kwargs):
  10. # ...
  11. if bad_param:
  12. raise BadParameter()

框架的某些部分会为您引发异常,例如,在验证失败时,会引发ValidationError

英文:

In both Django and in Django Rest Framework these type of things are dealt with by raising Exceptions:

  1. from rest_framework.exceptions import APIException
  2. class BadParameter(APIException):
  3. status_code = 400
  4. default_detail = 'The parameter x is wrong or empty'
  5. default_code = 'bad_param'
  6. # ...
  7. class PlantListByClientView(generics.ListAPIView):
  8. # ...
  9. def list(self, request, *args, **kwargs):
  10. # ...
  11. if bad_param:
  12. raise BadParameter()

Some parts of the framework will raise exceptions for you, like, for example, serializes rising ValidationError when validation fails.

huangapple
  • 本文由 发表于 2023年2月19日 12:02:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定