From 727f2d12829bde6f579bf4eacddfbd9adbd23a49 Mon Sep 17 00:00:00 2001 From: Ryan Noelk Date: Fri, 2 Mar 2018 16:03:13 -0500 Subject: [PATCH] using the slug in the URL (#2) --- v1/recipe/serializers.py | 7 +++++-- v1/recipe/views.py | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/v1/recipe/serializers.py b/v1/recipe/serializers.py index 34c2f64..740b392 100644 --- a/v1/recipe/serializers.py +++ b/v1/recipe/serializers.py @@ -38,15 +38,17 @@ def to_representation(self, value): class SubRecipeSerializer(serializers.ModelSerializer): """ Standard `rest_framework` ModelSerializer """ + slug = serializers.ReadOnlyField(source='child_recipe.slug') title = serializers.ReadOnlyField(source='child_recipe.title') class Meta: model = SubRecipe fields = ( + 'child_recipe_id', + 'slug', 'quantity', 'measurement', 'title', - 'child_recipe_id', ) @@ -59,6 +61,7 @@ class Meta: model = Recipe fields = ( 'id', + 'slug', 'title', 'pub_date', 'rating', @@ -87,7 +90,7 @@ def get_subrecipes(self, obj): class Meta: model = Recipe - exclude = ('slug',) + fields = '__all__' def update(self, instance, validated_data): """ diff --git a/v1/recipe/views.py b/v1/recipe/views.py index 090016b..0fbb3eb 100644 --- a/v1/recipe/views.py +++ b/v1/recipe/views.py @@ -21,6 +21,7 @@ class RecipeViewSet(viewsets.ModelViewSet): This viewset automatically provides `list`, `create`, `retrieve`, `update` and `destroy` actions. """ + lookup_field = 'slug' serializer_class = serializers.RecipeSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) filter_backends = (filters.SearchFilter, filters.OrderingFilter)