1

I want to create an object using the CreateAPIView from the django-rest-framework. When calling the view, I get a MemoryError. That's probably because the view tries to present all 350000 existing objects in the browseable response.

How should I prevent the view from performing the corresponding query? Defining a post or a get_queryset method does not help.

1 Answer 1

1

I solved the problem by using the APIView instead of the CreateAPIView. Here's the class I wrote:

class VoteCreateAPIView(views.APIView):
    def post(self, request, *args, **kwargs):
        vote = request.POST.get('vote', '')
        # here some validation
        Vote.objects.create(
            user=request.user,
            vote=vote)
        return response.Response({'vote': vote}, status=status.HTTP_200_OK)

I would still be curious if there's a better way to do it.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you find the proper way to do this or why this happens at all in the end? Do I understand correctly that here you are basically inheriting from the more generic APIView and then implementing your own post manually?
Yes. I did exactly what you say. And it was simple in the end. The reason for the problem was that the CreateAPIView by default creates a response which is huge if you have many objects. An alternative solution would be to inherit from the CreateAPIView and to modify the response and the query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.