0

I am creating a restapi view which should be a one and all endpoint for all the crud.But no matter what i do it always executes post method .The terminal shows me that for e.g the patch method has been executed but it always does post and therefore creates a new object and it also doesn't delete the object it shows me the correct status code but the object is still there.

Here's my view

class StatusGET_New(

generics.ListCreateAPIView,
mixins.RetrieveModelMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin

):

queryset = Status.objects.all()
serializer_class = StatusSerializers
permission_classes = []

def perform_destroy(self, instance):
    if instance is not None:
        return instance.delete()
    return None

def get_queryset(self):
    qs = Status.objects.all()
    query = self.request.GET.get('q')
    if query is not None:
        qs = qs.filter(

            content__icontains = query

            )
    return qs   



def get_object(self):
    request = self.request
    passed_id = request.GET.get('pk',None)
    queryset = self.get_queryset()
    obj = None
    if passed_id is not None:
        obj = get_object_or_404(queryset,pk = passed_id)
        self.check_object_permissions(request,obj)
    return obj



def get(self,request,*args,**kwargs):
    passed_id = self.request.GET.get('pk',None)

    if passed_id is not None:
        return self.retrieve(request,*args,**kwargs)
    return super().get(request,*args,**kwargs)  


def post(self,request,*args,**kwargs):
    return self.create(request,*args,**kwargs)


def put(self,request,*args,**kwargs):
    url_passed_id = request.GET.get("pk",None)
    json_data = {}
    body_     = request.body
    if is_json(body_):
        json_data = json.loads(request.body)
    new_passed_id = json_data.get('pk',None)

    passed_id = url_passed_id or new_passed_id or None
    self.passed_id = passed_id
    return self.update(request,*args,**kwargs)



def patch(self,request,*args,**kwargs):
    url_passed_id = request.GET.get("pk",None)
    json_data = {}
    body_     = request.body
    if is_json(body_):
        json_data = json.loads(request.body)
    new_passed_id = json_data.get('pk',None)

    passed_id = url_passed_id or new_passed_id or None
    self.passed_id = passed_id
    return self.partial_update(body_,*args,**kwargs)




def delete(self,request,*args,**kwargs):
    url_passed_id = request.GET.get("pk",None)
    json_data = {}
    body_     = request.body
    if is_json(body_):
        json_data = json.loads(request.body)
    new_passed_id = json_data.get('pk',None)

    passed_id = url_passed_id or new_passed_id or None
    self.passed_id = passed_id
    return self.destroy(request,*args,**kwargs)

1 Answers1

0

You're mixing up the ListCreateAPIView and mixins. You should instead use the GenericAPIView together with mixins and usually the mixins should come first in the inheritance order before the GenericAPIView.

class StatusGET_New(
    generics.GenericAPIView,
    mixins.RetrieveModelMixin,
    mixins.CreateModelMixin,
    mixins.UpdateModelMixin,
    mixins.DestroyModelMixin):
...

But from your code, you seem to want to have both detail route and list route handled by the same view, so what you need is the viewset. So it should be like this:

from restframework.viewsets import ModelViewSet

class StatusGET_New(ModelViewSet):
    ...

The model viewet already has implementation for all those methods so you may want to check the code and use the default implementation where possible.

And just as a comment, you could really improve your view naming - something like StatusView or StatusViewSet

Ken4scholars
  • 6,076
  • 2
  • 21
  • 38