Как изменить запрос до вызова класса View?

У меня уже есть ModelViewSet.

class ConfigManager(ModelViewSet):
    queryset = Configuration.objects.all()
    serializer_class = ConfigSerializer
    http_method_names = ["post", "get", "patch", "delete"]

    def perform_create(self, serializer):
        if Configuration.objects.count():
            raise BadRequest(
                message="Only 1 Configuration is allowed. Try updating it."
            )
        obj = serializer.save()
        tenant_id = self.request.META.get("HTTP_X_TENANT_ID")
        cache_config(obj, tenant_id)

    def perform_update(self, serializer):
        obj = serializer.save()
        tenant_id = self.request.META.get("HTTP_X_TENANT_ID")
        cache_config(obj, tenant_id)

    def perform_destroy(self, instance):
        if Configuration.objects.count() <= 1:
            raise BadRequest(message="One configuration object must exist.")
        instance.delete()
        obj = Configuration.objects.all().first()
        tenant_id = self.request.META.get("HTTP_X_TENANT_ID")
        cache_config(obj, tenant_id)

Теперь я пытаюсь создать обертку для этого класса ViewSet, где я хочу выполнить некоторые модификации запроса перед его обслуживанием. Как я могу это сделать?

class ConfigManagerWrapper(ConfigManager):
   # perform modification to request.
   # request.data._mutable = True
   # request.data['customer'] = tenant_id

Простите, я не знаком с тем, как использовать ModelViewSet, но вы можете сделать это, используя представления, основанные на функциях, следующим образом:

@api_view('POST'):
def post_view(request):
    queryset = Configuration.objects.all()
    data = request.data
    post_serializer = ConfigSerializer(data)
    data._mutable = True
    data.get("customer") = tenant.id
    if post_serializer.is_valid():
        post_serializer.save()
        return Response({})
    else:
        return Response({})
Вернуться на верх