Skip to content

Commit

Permalink
feat: adding post endpoint to create new products and updating serial…
Browse files Browse the repository at this point in the history
…izer to return id
  • Loading branch information
ebauschatz committed Sep 23, 2022
1 parent f72ff6d commit 4ff13e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion products/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ['title', 'description', 'price', 'inventory_quantity']
fields = ['id', 'title', 'description', 'price', 'inventory_quantity']
8 changes: 7 additions & 1 deletion products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
from .serializers import ProductSerializer


@api_view(['GET'])
@api_view(['GET', 'POST'])
def products_list(request):
if request.method == 'GET':
products = Product.objects.all()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

elif request.method == 'POST':
serializer = ProductSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)

@api_view(['GET'])
def product_detail(request, pk):
product = get_object_or_404(Product, pk = pk)
Expand Down

0 comments on commit 4ff13e7

Please sign in to comment.