From 41b6b82a8f8c87b218e1d47131ee269c0cccffdd Mon Sep 17 00:00:00 2001 From: Nikki Laude <72233907+nikkilaude@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:20:15 -0800 Subject: [PATCH] update vehicle (#28) --- backend/vehicle/views.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/vehicle/views.py b/backend/vehicle/views.py index e51c267..0819be0 100644 --- a/backend/vehicle/views.py +++ b/backend/vehicle/views.py @@ -41,7 +41,8 @@ def post(self, request, *args, **kwargs): brand = get_object_or_404(Brand, id=brand_id) if brand_id else None vehicle_type = get_object_or_404(Type, id=type_id) if type_id else None - vehicle = Vehicle.objects.create(brand=brand, vehicle_type=vehicle_type, **data) + vehicle = Vehicle.objects.create( + brand=brand, vehicle_type=vehicle_type, **data) # Use the serializer class's data directly serialized_data = self.serializer_class(vehicle) return Response(serialized_data.data, status=status.HTTP_201_CREATED) @@ -64,6 +65,19 @@ def get(self, request, vehicle_id, *args, **kwargs): except vehicle.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) + def put(self, request, vehicle_id, format=None): + """ + Update specific vehicle + + """ + vehicle = get_object_or_404(Vehicle, id=vehicle_id) + serializer = VehicleSerializer( + vehicle, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + def delete(self, request, vehicle_id, format=None): """ Delete specific vehicle @@ -91,7 +105,8 @@ def get(self, request): serialized_data = VehicleSerializer(vehicles, many=True) return Response( - {"vehicles": serialized_data.data, "more_data": has_more_data(request)} + {"vehicles": serialized_data.data, + "more_data": has_more_data(request)} ) return Response(VehicleSerializer(Vehicle.objects.all()[:10], many=True).data)