I am trying to construct a class based Django REST API that takes in data from the POST request and outputs the relevent data.
I have a list of buildings in the DB
State Building
California Golden Gate Bridge
New York Empire State
California TransAmerica
New York Brooklyn Bridge
User will supply the state through a POST Request (i.e. California), the API should return the appropriate building (i.e. Golden Gate Bridge, TransAmerica)
I guess I'm just having a hard time understand how the class is taking in the request.data ['state_id']. Should I use APIView? So many questions!
So far I have this in my view.py
class BuildingViewSet(viewsets.ModelViewSet):
queryset = BuildingRule.objects.values('building_name').distinct()
serializer_class = BuildingSerializer
def post(self, request, *args, **kwargs):
queryset = BuildingRule.objects.filter(state_name=request.data['state_id'])
return Response(queryset.values_list('building_name', flat=True))
This is in my serializer.py
class BuildingSerializer(serializers.ModelSerializer):
class Meta:
model = BuildingRule
fields = ('building_name',)
This is my url.py:
router.register(r'buildings', views.BuildingsViewSet, base_name='Building')