2

I am using the Django REST framework in order to implement a game server for an android game. I have written a class, that is derived from GenericAPIView to handle a specific Http Post request. I want the request to return a list of some objects, that were previously queried from the database.

My code looks like this:

class NewGameView(GenericAPIView):
    serializer_class=NewGameRequestSerializer
    def post(self, request, format=None):
        serializer = NewGameRequestSerializer(data=request.DATA)
        if serializer.is_valid():
            req=serializer.save()

            mygamedata=...; # request game data objects

            serializer = MyGameDataSerializer(mygamedata, many=True)
            return Response(serializer.data,status=status.HTTP_201_CREATED)
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)

When I access this view via curl, everything works as expected. However, when I submit using the Django generated "browsable api" form, I am receiving a 'ListSerializer' object is not iterable error during "template rendering". If I derive my class from APIView instead, the error does not occur, but Django will not display the forms anymore (for whatever reason).

Can anyone explain what is going on?

Thanks

2
  • there is no ListSerializer variable in your code. can you provide further code? Commented Jun 7, 2015 at 12:43
  • The ListSerializer class is nothing I have written. It is part of the Django REST framework. My MyGameDataSerializer is derived from serializers.ModelSerializer. I believe that passing many=True automatically makes it use this ListSerializer in some way. I am unsure about the exact internals though (I am fairly new to Django REST). Commented Jun 7, 2015 at 12:53

1 Answer 1

3

You can just return a dictionary with the data you need.

class NewGameView(GenericAPIView):
serializer_class=NewGameRequestSerializer
def post(self, request, format=None):
    serializer = NewGameRequestSerializer(data=request.DATA)
    if serializer.is_valid():
        req=serializer.save()

        mygamedata=...; # request game data objects

        data = {'game_name': mygame_object.name}
        return Response(data,status=status.HTTP_201_CREATED)
Sign up to request clarification or add additional context in comments.

9 Comments

Well I did not use the ModelViewSet class, because my API interface does not really have a 1:1 correspondence to the database structure. My NewGameRequest class for example does not correspond to any model. Nevertheless, I did try what you said, and after also changing the url pattern to: url(r'^new_game/', views.NewGameView.as_view({'post': 'post'})) it showed the exact same problem as it did before.
@Dtag so you are not trying to create a new database entry with the post?
Change your url to url(r'^new_game/',views.NewGameView.as_view())
The error occurs on this line serializer = MyGameDataSerializer(mygamedata, many=True), if you remove the many=True it should fix the issue, you can also just return a dictionary with all the data
The issue is that the browsable api can't render the response so id you return data={'name': 'GameData'} as the response you wont get the error, I'll update my answer to show you what I meant
|

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.