Updated part API

This commit is contained in:
Oliver 2017-04-11 23:33:08 +10:00
parent 08fc21eb9b
commit 4c07e8d8a0
3 changed files with 22 additions and 25 deletions

View File

@ -3,41 +3,25 @@ from rest_framework import serializers
from .models import Part, PartCategory, PartParameter
class ParameterSerializer(serializers.ModelSerializer):
class PartParameterSerializer(serializers.ModelSerializer):
""" Serializer for a PartParameter
"""
class Meta:
model = PartParameter
fields = ('pk',
'part',
'template',
'name',
'value',
'units')
class PartDetailSerializer(serializers.ModelSerializer):
class PartSerializer(serializers.ModelSerializer):
""" Serializer for complete detail information of a part.
Used when displaying all details of a single component.
"""
params = ParameterSerializer(source='parameters', many=True)
class Meta:
model = Part
fields = ('pk',
'name',
'IPN',
'description',
'category',
'stock',
'params')
class PartBriefSerializer(serializers.ModelSerializer):
""" Serializer for displaying overview of a part.
Used e.g. for displaying list of parts in a category.
"""
class Meta:
model = Part
fields = ('pk',
@ -60,7 +44,7 @@ class PartCategoryBriefSerializer(serializers.ModelSerializer):
class PartCategoryDetailSerializer(serializers.ModelSerializer):
# List of parts in this category
parts = PartBriefSerializer(many=True)
parts = PartSerializer(many=True)
# List of child categories under this one
children = PartCategoryBriefSerializer(many=True)

View File

@ -6,6 +6,9 @@ urlpatterns = [
# Single part detail
url(r'^(?P<pk>[0-9]+)/$', views.PartDetail.as_view()),
# Part parameters list
url(r'^(?P<pk>[0-9]+)/parameters/$', views.PartParameters.as_view()),
# Part category detail
url(r'^category/(?P<pk>[0-9]+)/$', views.PartCategoryDetail.as_view()),

View File

@ -1,20 +1,30 @@
from rest_framework import generics
from .models import PartCategory, Part
from .serializers import PartBriefSerializer, PartDetailSerializer
from .models import PartCategory, Part, PartParameter
from .serializers import PartSerializer
from .serializers import PartCategoryBriefSerializer, PartCategoryDetailSerializer
from .serializers import PartParameterSerializer
class PartDetail(generics.RetrieveAPIView):
queryset = Part.objects.all()
serializer_class = PartDetailSerializer
serializer_class = PartSerializer
class PartParameters(generics.ListAPIView):
def get_queryset(self):
part_id = self.kwargs['pk']
return PartParameter.objects.filter(part=part_id)
serializer_class = PartParameterSerializer
class PartList(generics.ListAPIView):
queryset = Part.objects.all()
serializer_class = PartBriefSerializer
serializer_class = PartSerializer
class PartCategoryDetail(generics.RetrieveAPIView):