-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf54019
commit 698be50
Showing
4 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import serializers | ||
|
||
from .models import Vehicle | ||
|
||
|
||
class VehicleSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Vehicle | ||
fields = '__all__' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.contrib import admin | ||
from django.urls import include, path | ||
|
||
from vehicle import views | ||
|
||
urlpatterns = [ | ||
path("", views.VehicleAPIView.as_view(), name="vehicle"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,34 @@ | ||
from django.shortcuts import render | ||
from rest_framework import status, generics | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
# Create your views here. | ||
from .models import Vehicle, Brand, Type | ||
from .serializers import VehicleSerializer | ||
|
||
|
||
class VehicleAPIView(APIView): | ||
serializer_class = VehicleSerializer | ||
|
||
def get(self, request, *args, **kwargs): | ||
""" | ||
Get specific vehicle | ||
""" | ||
identifier = request.data.get("id") | ||
vehicle = Vehicle.objects.get(id=identifier) | ||
serialized_data = self.serializer_class(vehicle) | ||
try: | ||
return Response(serialized_data.data, status=status.HTTP_200_OK) | ||
except vehicle.DoesNotExist: | ||
return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
||
def post(self, request, *args, **kwargs): | ||
data = request.data | ||
brand_id = data.get("brand") | ||
type_id = data.get("type") | ||
data.pop("brand") | ||
data.pop("type") | ||
brand = Brand.objects.get(id=brand_id) | ||
type = Type.objects.get(id=type_id) | ||
vehicle = Vehicle.objects.create(**data, brand=brand, vehicle_type=type) | ||
serialized_data = self.serializer_class(vehicle) | ||
return Response(serialized_data.data, status=status.HTTP_200_OK) |