I am hoping to return ids of newly created objects after deserialization. However, to do this, I have to serialize all newly created data again. If I don't, I get the data back but without the ids.
Isn't REST supposed to work that way - return to the caller what was created? If it should, am I missing something? The DRF tutorial left me with the impression that this should work (return the ids).
Here is code:
class ProjectView(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def put(self, request, *args, **kwargs):
ser = ProjectCreateSerializer(data=request.data, context={'user': request.user})
if ser.is_valid(raise_exception=True):
proj = ser.save(user=request.user)
#### The line below is required to get ids back to the user
ser = ProjectCreateSerializer(proj)
return Response(ser.data, status=status.HTTP_201_CREATED)
return Response(status=status.HTTP_400_BAD_REQUEST)
I also have a create method on the ProjectCreateSerializer
and it creates and saves the object before returning it.
EDIT
I would like to be able to
PUT /project/
{
'name': 'Demo'
}
and get back:
{
'id': 1,
'name': 'Demo'
}
in one shot, without deserialize --> data --> serialize.