1

My code for receiving data and then returning the response is:

serializer = VideoSerializer(data=data, partial=True)
    if serializer.is_valid():
        serializer.save()
        return JSONResponse(serializer.data, status=201)
    return JSONResponse(serializer.errors, status=400)

Here if the serializer is valid I want to return partial data from the serializer(after removing some fields).

It could be done if I create another serializer object and pass fields tuple to it as given here https://stackoverflow.com/a/23674297/1694699 but that would mean another database call also I will have to query for the id to pass it to the new serializer object.

Is their any better approach?

Community
  • 1
  • 1
ofnowhere
  • 1,114
  • 2
  • 16
  • 40
  • 2
    May be `write_only` field parameter helps? – sinitsynsv Sep 23 '15 at 09:37
  • I am not sure I get the problem. Is it not working by removing the fields from `serializer.validated_data` ? (or a copy of it). BTW `partial` is for [updates](http://www.django-rest-framework.org/api-guide/serializers/#partial-updates), not for creating a new instance, right? – Pynchia Sep 23 '15 at 09:40
  • @user2172884 got it working using `write_only` parameter, thanks. – ofnowhere Sep 23 '15 at 09:48

0 Answers0