Save creation user when making a new part via the API

This commit is contained in:
Oliver Walters 2020-03-18 22:00:32 +11:00
parent a147ce4284
commit d51ac2f5c2

View File

@ -126,6 +126,24 @@ class PartList(generics.ListCreateAPIView):
serializer_class = part_serializers.PartSerializer
def create(self, request, *args, **kwargs):
""" Override the default 'create' behaviour:
We wish to save the user who created this part!
Note: Implementation coped from DRF class CreateModelMixin
"""
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
# Record the user who created this Part object
part = serializer.save()
part.creation_user = request.user
part.save()
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def list(self, request, *args, **kwargs):
"""
Instead of using the DRF serialiser to LIST,