Django serializer return partial data

半世苍凉 提交于 2019-12-13 00:03:50

问题


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?

来源:https://stackoverflow.com/questions/32735511/django-serializer-return-partial-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!