In urls.py

url(r'^someurl/(?P<pk>\d+)/', MyApi.as_view(), name='delete') 

where name='delete' is the name of the function

Those. URL will be

 ...someurl/123/ 

In views.py

 def delete(self, request, pk, format=None): item = self.model.objects.get(pk=pk) item.delete() print(pk) return Response(status=status.HTTP_204_NO_CONTENT) 

In the controller I cause

 $http.delete('someurl/' + id + '/'); 

With such a request, the object with the number = id will be deleted.

But now I need to delete an object without knowing its id . I only know the value of the image_url field.

Call

 $http.delete('someurl/1/'); 

where 1 is any number not associated with anything, because delete requires such a call format; and pass image_url as an optional parameter to request? In this way:

 $http.delete('someurl/1/', {'image_url': 'какой-то урл'}); 

Is there a more elegant solution than attributing a magic number at the end?

Update:

Using

 $http.delete('someurl/1/', {'image_url': 'какой-то урл'}); 

and in view.py

 def delete(self, request, pk, format=None): image_url = request.data['image_url'] item = self.model.objects.get(image_url=image_url) item.delete() return Response(status=status.HTTP_204_NO_CONTENT) 

Error 403 occurs.

Update 2

 class SortedItemsAPI(mixins.UpdateModelMixin, generics.ListCreateAPIView): model = SortedItem permission_classes = [ permissions.IsAuthenticated ] def get_serializer_class(self): if self.request.method in ['PUT', 'POST']: return SortedItemIdSerializerBasic return SortedItemSerializer def get_queryset(self): return self.model.objects.filter(user=self.request.user).order_by('order') def create(self, request, *args, **kwargs): data = {'user': request.user.id} data.update(request.data) serializer = self.get_serializer(data=data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) def put(self, request, *args, **kwargs): for order, element in enumerate(request.data['sorted']): self.model.objects.filter( user=request.user, id=element).update( order=order ) print(request.data['sorted']) return Response(status=status.HTTP_200_OK) def delete(self, request, pk, format=None): item = self.model.objects.get(item_id=pk) item.delete() return Response(status=status.HTTP_204_NO_CONTENT) 

If I change to

  def delete(self, request, pk, format=None): item = self.model.objects.get(item_id=request.GET['id']) item.delete() return Response(status=status.HTTP_204_NO_CONTENT) 

and, of course, I pass the id parameter to request.data , which is exactly equal to pk - an 500 INTERNAL SERVER ERROR error 500 INTERNAL SERVER ERROR occurs.

  • one
    You can make a separate url, in which you can delete images by id or another unique attribute - FeroxTL
  • @FeroxTL how to add url to call your own function? - Emm
  • What does own function mean? If you want to get the url for the instance of a specific model, then you can override its method get_absolute_url and call it - FeroxTL
  • @FeroxTL not really. You wrote above about "make a separate url", please specify what you had in mind? If I understand correctly, for this separate url do you need your own method in the view for the API? Or override delete ()? - Emm
  • Tell item = self.model.objects.get(image_url=image) field you have and why you make item = self.model.objects.get(image_url=image) if you don’t have an image defined - FeroxTL

1 answer 1

If you use rest_framework , you can use the @list_route decorator

  @list_route(method=['delete']) def remove_image(self, request): image_url = request.data['image_url'] item = self.model.objects.get(image_url=image_url) item.delete() return Response(status=status.HTTP_204_NO_CONTENT)