1

I am having trouble sending a delete request from axios to my Django backend. With axios I call delete in the following way for many of my models:

// DELETE DAY
export const deleteDay = (day_id) => (dispatch, getState) => {
  axios
    .delete(`/api/days/${day_id}/`, tokenConfig(getState))
    ...

The value of tokenConfig(getState) is just:

{
  headers: {
    Authorization: "Token 032b2f569037f0125753ef8f67e7774d34a756646ae28cefd54eb1c54bd0b149"
    Content-type: "application/json"
  }
}

In my api.py file I have many viewsets which use my predefined model and serializer objects. The viewset that should catch this request is the DayViewSet. I followed a guide to create all of these viewsets and from what I could find online, it seems that the guide I used uses GenericAPIView functions. For this reason I've been using functions like perform_create, get_queryset, etc.

I have been able to successfully call this deleteDay function, and it would just delete the instance of the object based on the day_id I gave it, but recently, after changing my get_queryset function, it seems that a strange issue has arisen. Now, when I call the deleteDay function, it calls my get_queryset function instead of whatever is normal. Here is the DayViewSet object:

class DayViewSet(viewsets.ModelViewSet):
    permission_classes = [
        permissions.IsAuthenticated
    ]

    serializer_class = DaySerializer

    def get_queryset(self):

        day_filter = self.request.GET.get("day_filter", None)

        if day_filter == 'last':
            all_queryset = Day.objects.all()
            ordered_all_queryset = all_queryset.order_by("-day_id")
            if ordered_all_queryset.count() == 0:
                return []
            else:
                return [ordered_all_queryset.first()]

        if day_filter == 'first':
            all_queryset = Day.objects.all()
            ordered_all_queryset = all_queryset.order_by("day_id")
            if ordered_all_queryset.count() == 0:
                return []
            else:
                return [ordered_all_queryset.first()]

        eventdefinitions = self.request.user.eventdefinitions.all()
        strictevents = StrictEvent.objects.filter(
            event_id__in=eventdefinitions)
        looseevents = LooseEvent.objects.filter(event_id__in=eventdefinitions)
        occurs_on_1s = occurs_on_1.objects.filter(event_id__in=strictevents)
        occurs_on_2s = occurs_on_2.objects.filter(event_id__in=looseevents)
        occurs_on_s = occurs_on_1s | occurs_on_2s
        queryset = Day.objects.filter(day_id__in=occurs_on_s)

        return queryset

    def perform_create(self, serializer):
        serializer.save()

So the issue is that whenever I call the deleteDay function it always calls the get_queryset function in my viewset and I'm not sure why. I tried adding a perform_destroy function, hoping that it would overwrite the delete request, but it's not overwriting it. So I have two questions, why is the get_queryset function being triggered when I call a delete request, and what can I do to make it trigger the perform_destroy function (or something comparable) instead.

FlamePrinz
  • 490
  • 1
  • 5
  • 20
  • There is not much we can do, if there are cascading deletes required. still the question and answers are informative. I had once wondered why should queryset.delete query use this much resource – Mohammed Shareef C Aug 16 '23 at 19:17

2 Answers2

1

Because when you call delete method, we must know which object will be deleted.

So we will call default get_object method in modelviewset as default method.

And then get_object calls get_queryset.

Therefore if you don't want to call get_queryset in get_object method.

Just override get_object not to call get_queryset.

Read for more understanding: https://github.com/encode/django-rest-framework/blob/master/rest_framework/generics.py#L83

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sam Kim
  • 113
  • 1
  • 9
1

This is intended !!!

DeleteView is a kind of detail view which requires to fetch the object to be deleted using the get_object(...) method. This get_object(...) will call the get_queryset(...) method to fetch which all objects are allowed to perform in a particular view.

In your case, you are overriding the get_queryset(...) method for filtering purposes. You should override the filter_queryset(...) method to aplly the filters.

JPG
  • 82,442
  • 19
  • 127
  • 206