I have a a ModelViewSet
in Django's REST Framework that gives me the ability to do a POST and GET through the following address:
api/v1/users
These Users have a a reverse relation to a Comments table and I'd like to be able to access these Comments through the URL:
api/v1/users/<username>/comments
I was able to do this by setting up a custom @detail_route
by overriding the queryset
and and serializer_class
but it only works for GET requests. If I attempt to POST a new comment through the REST Framework's admin section, I receive an error notice stating "'ListSerializer' object is not iterable"
Here's my code:
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserFlat
lookup_field = 'username'
@detail_route(methods=['post','get'])
def comment(self, request, **kwargs):
user = self.get_object()
self.queryset = Comment.objects.filter(recipient=user.id)
self.serializer_class = CommentFlat
serializer = CommentFlat(instance=self.queryset, many=True)
return Response(serializer.data)
The new comment should take the following parameters:
comment.author, comment.message, comment.recipient
I can enter all those fields in the admin panel and attempt to create a post request but it fails. Ideally, I should only have to fill out comment.message and comment.author and the comment.recipient field should be autopopulated with user.id which is obtained in the url api/v1/users/<username>/comments
Does anyone know why my post request isn't completing and how I can autofill that one field?
I figured it out. For those looking for an answer as well, the solution was to explicitly define the actions that occur when
request.method == 'POST'
and pass a the object into the serializer.