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'>`
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.
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.
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.
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.
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.
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.
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.
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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With