[Fixed]-How do I add a field to a django-rest-framework serializer that isn't on my model?

1👍

You could probably use another Serializer and use it for you PUT API

StatisticsUpdateSerializer:

class StatisticsUpdateSerializer:
    is_winner = ...
    num_hands_won = ...
    score = ...

And use this serializer in the PUT API or create a new route as shown in the example mentioned in the DRF documentation here

@detail_route(methods=['post'])
def set_password(self, request, pk=None):
    user = self.get_object()

    // Use your serializer below
    serializer = PasswordSerializer(data=request.data)

    if serializer.is_valid():
        user.set_password(serializer.data['password'])
        user.save()
        return Response({'status': 'password set'})
    else:
        return Response(serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
👤Shetty

Leave a comment