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.