I created an API
on Django
using the rest framework
, I populate that through the default Django Admin Page
.
I've some fields in my API
, here my serializers.py
:
class HCSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = HomeController
fields = ('full_name','Staff','CID', 'OI', 'Rating', 'GND', 'TWR', 'APP', 'CTR', 'Notes')
class VCSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = VisitingController
fields = ('full_name','CID', 'OI', 'Rating', 'GND', 'TWR', 'APP', 'CTR', 'Notes')
my views.py
:
class HCViewSet(APIView):
def get(self):
HC = HomeController.objects.all().order_by('full_name')
serializer = HCSerializer(HC, many=True)
return Response(serializer.data)
class VCViewSet(APIView):
def get(self):
VC = VisitingController.objects.all().order_by('full_name')
serializer = VCSerializer(VC, many=True)
return Response(serializer.data)
my models.py
:
class HomeController(models.Model):
full_name = models.CharField(max_length=60)
ATM = 'ATM'
DATM = 'DATM'
TA = 'TA'
EC = 'EC'
FE = 'FE'
WM = 'WM'
INS = 'INS'
MTR = 'MTR'
BLK = 'BLK'
Staff = [
(ATM, 'Air Traffic Manager'),
(DATM, 'Deputy Air Traffic Manager'),
(TA, 'Training Administrator'),
(EC, 'Events Coordinator'),
(FE, 'Facility Engeneer'),
(WM, 'Webmaster'),
(INS, 'Instructor'),
(MTR, 'Mentor'),
(BLK, 'Blank'),
]
Staff = models.CharField(
max_length=100,
choices=Staff,
default=BLK
)
CID = models.CharField(max_length=60)
OI = models.CharField(max_length=10, default='XX')
Rating = models.CharField(max_length=10)
NOT_CERTIFIED = 'NC'
IN_TRAINING = 'IT'
SOLO_VALIDATION = 'SV'
CERTIFIED = 'CE'
Certs = [
(NOT_CERTIFIED, 'Not certified'),
(IN_TRAINING, 'In Training'),
(SOLO_VALIDATION, 'Solo Validation'),
(CERTIFIED, 'Certified'),
]
GND = models.CharField(
max_length=2,
choices=Certs,
default=NOT_CERTIFIED,
)
TWR = models.CharField(
max_length=2,
choices=Certs,
default=NOT_CERTIFIED,
)
APP = models.CharField(
max_length=2,
choices=Certs,
default=NOT_CERTIFIED,
)
CTR = models.CharField(
max_length=2,
choices=Certs,
default=NOT_CERTIFIED,
)
Notes = models.CharField(max_length=360, blank=True)
class VisitingController(models.Model):
full_name = models.CharField(max_length=60)
CID = models.CharField(max_length=60)
OI = models.CharField(max_length=10, default='XX')
Rating = models.CharField(max_length=10)
NOT_CERTIFIED = 'NC'
IN_TRAINING = 'IT'
SOLO_VALIDATION = 'SV'
CERTIFIED = 'CE'
GND_Certs = [
(NOT_CERTIFIED, 'Not certified'),
(IN_TRAINING, 'In Training'),
(SOLO_VALIDATION, 'Solo Validation'),
(CERTIFIED, 'Certified'),
]
GND = models.CharField(
max_length=2,
choices=GND_Certs,
default=NOT_CERTIFIED,
)
TWR_Certs = [
(NOT_CERTIFIED, 'Not certified'),
(IN_TRAINING, 'In Training'),
(SOLO_VALIDATION, 'Solo Validation'),
(CERTIFIED, 'Certified'),
]
TWR = models.CharField(
max_length=2,
choices=GND_Certs,
default=NOT_CERTIFIED,
)
APP_Certs = [
(NOT_CERTIFIED, 'Not certified'),
(IN_TRAINING, 'In Training'),
(SOLO_VALIDATION, 'Solo Validation'),
(CERTIFIED, 'Certified'),
]
APP = models.CharField(
max_length=2,
choices=GND_Certs,
default=NOT_CERTIFIED,
)
CTR_Certs = [
(NOT_CERTIFIED, 'Not certified'),
(IN_TRAINING, 'In Training'),
(SOLO_VALIDATION, 'Solo Validation'),
(CERTIFIED, 'Certified'),
]
CTR = models.CharField(
max_length=2,
choices=GND_Certs,
default=NOT_CERTIFIED,
)
Notes = models.CharField(max_length=360, blank=True)
def __str__(self):
return self.full_name
My question: the API
works fine and I'm able to get all the results, but I'm looking to create an endopoint
like /api/<int:CID>
that returns just one user. I would like to use the field 'CID
' to filter. How can I do that? Thanks!
CodePudding user response:
And you need to create the detail views.
from django.http import Http404
class HCDetailViewSet(APIView):
def get_object(self, CID):
try:
return HomeController.objects.get(CID=CID)
except HomeController.DoesNotExist:
raise Http404
def get(self, request, CID, format=None):
snippet = self.get_object(CID)
serializer = HCSerializer(snippet)
return Response(serializer.data)
class VCDetailViewSet(APIView):
def get_object(self, CID):
try:
return VisitingController.objects.get(CID=CID)
except VisitingController.DoesNotExist:
raise Http404
def get(self, request, CID, format=None):
snippet = self.get_object(CID)
serializer = VCSerializer(snippet)
return Response(serializer.data)
And in urls.py, you have to add the URLs for the detail views.
path('hc/<str:CID>', HCDetailViewSet.as_view(), name="hc_detail_view")
path('vc/<str:CID>', VCDetailViewSet.as_view(), name="vc_detail_view")