Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override create method in django rest generics CreateAPIView

My views.py of django app is as below,

class MemberCreate(generics.CreateAPIView):
    queryset = members.objects.all()
    serializer_class = MemberSerializer
    permission_classes = (permissions.IsAdminUser,)

    def create(self, serializer):
        ''' I wanted to do some stuff with serializer.data here '''
        pass 

Here in the above if I override the create function the return is failing with following error, Even If I don't do any thing and just write pass it is failing ,

AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`
like image 582
Naggappan Ramukannan Avatar asked Aug 31 '17 12:08

Naggappan Ramukannan


People also ask

What is difference between APIView and GenericAPIView?

APIView is a base class. It doesn't assume much and will allow you to plug pretty much anything to it. GenericAPIView is meant to work with Django's Models. It doesn't assume much beyond all the bells and whistles the Model introspection can provide.

Should either include a Serializer_class attribute or override the?

serializer_class - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the get_serializer_class() method.

What is Perform_create in Django?

perform_create. perform create method is used to add extra information when creating a new object. perform_create() method will not execute if you override create() method.

What is QuerySet in Django REST framework?

The root QuerySet provided by the Manager describes all objects in the database table. Usually, though, you'll need to select only a subset of the complete set of objects. The default behavior of REST framework's generic list views is to return the entire queryset for a model manager.

How can I reuse the generic views in my API?

If the generic views don't suit the needs of your API, you can drop down to using the regular APIView class, or reuse the mixins and base classes used by the generic views to compose your own set of reusable generic views. Typically when using the generic views, you'll override the view, and set several class attributes.

What is genericapiview class in rest?

This class extends REST framework's APIView class, adding commonly required behavior for standard list and detail views. Each of the concrete generic views provided is built by combining GenericAPIView, with one or more mixin classes. The following attributes control the basic view behavior.

What are generic views in Django?

Generic views. Django’s generic views... were developed as a shortcut for common usage patterns... They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself.

How do I override a generic view class?

Typically when using the generic views, you'll override the view, and set several class attributes. For more complex cases you might also want to override various methods on the view class. For example. For very simple cases you might want to pass through any class attributes using the .as_view () method.


2 Answers

The return should be instanceof Response.You can return parent class response, after your stuff

def create(self, request, *args, **kwargs):
    ''' I wanted to do some stuff with serializer.data here '''
    return super(MemberCreate, self).create(request, *args, **kwargs)

or else if you don't want the parent response, then simply return a Response instance

def create(self, request, *args, **kwargs):
    ''' I wanted to do some stuff with serializer.data here '''
    return Response(status=204)
like image 56
Rajez Avatar answered Oct 17 '22 16:10

Rajez


Your view should return a Response object, as said in your AssertionError stacktrace.

In your case, you can try to return an empty Response in order to test your view and your serializer.data

from rest_framework.response import Response
from rest_framework import status


class MemberCreate(generics.CreateAPIView):
    queryset = members.objects.all()
    serializer_class = MemberSerializer
    permission_classes = (permissions.IsAdminUser,)

    def create(self, request, *args, **kwargs):
        serializer = self.serializer_class(...)
        data = serializer.data

        # ...

        return Response(status=status.HTTP_204_NO_CONTENT)

Typically, you want to return your serializer.validated_data in the end, so this line probably will look like this:

return Response(serializer.validated_data, status=status.HTTP_201_CREATED)
like image 45
wencakisa Avatar answered Oct 17 '22 17:10

wencakisa