model changes

This commit is contained in:
2024-03-03 11:57:08 +03:00
parent 84faba9759
commit 5f80979aa2
6 changed files with 62 additions and 78 deletions
+22 -22
View File
@@ -1,49 +1,49 @@
from rest_framework import status, viewsets
from rest_framework.response import Response
from .models import Brand, Product, Supplier
from .serializers import BrandSerializer, ProductSerializer, SupplierSerializer
from .models import Product
from .serializers import ProductSerializer
class BrandViewSet(viewsets.ModelViewSet):
queryset = Brand.objects.all()
serializer_class = BrandSerializer
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
def list(self, request, *args, **kwargs):
data = list(Brand.objects.all().values())
data = list(Product.objects.all().values())
return Response(data, status=status.HTTP_200_OK)
def retrieve(self, request, *args, **kwargs):
data = list(Brand.objects.filter(id=kwargs["pk"]).values())
data = list(Product.objects.filter(id=kwargs["pk"]).values())
return Response(data, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs):
brand_serializer_data = BrandSerializer(data=request.data)
if brand_serializer_data.is_valid():
brand_serializer_data.save()
product_serializer_data = ProductSerializer(data=request.data)
if product_serializer_data.is_valid():
product_serializer_data.save()
status_code = status.HTTP_201_CREATED
return Response({"message": "Brand added successfully"}, status=status_code)
return Response({"message": "Product added successfully"}, status=status_code)
else:
status_code = status.HTTP_400_BAD_REQUEST
return Response({"message": "please fill the details"}, status=status_code)
def destroy(self, request, *args, **kwargs):
brand_data = Brand.objects.filter(id=kwargs["pk"])
if brand_data:
brand_data.delete()
product_data = Product.objects.filter(id=kwargs["pk"])
if product_data:
product_data.delete()
status_code = status.HTTP_201_CREATED
return Response({"message": "Brand deleted succesfully"}, status=status_code)
return Response({"message": "Product deleted successfully"}, status=status_code)
else:
status_code = status.HTTP_400_BAD_REQUEST
return Response({"message": "Brand not found"}, status=status_code)
return Response({"message": "Product not found"}, status=status_code)
def update(self, request, *args, **kwargs):
brand_details = Brand.objects.get(id=kwargs["pk"])
brand_serializer_data = BrandSerializer(brand_details, data=request, partial=True)
if brand_serializer_data.is_valid():
brand_serializer_data.save()
product_details = Product.objects.get(id=kwargs["pk"])
product_serializer_data = ProductSerializer(product_details, data=request, partial=True)
if product_serializer_data.is_valid():
product_serializer_data.save()
status_code = status.HTTP_201_CREATED
return Response({"message": "Brand updated successfully"}, status=status_code)
return Response({"message": "Product updated successfully"}, status=status_code)
else:
status_code = status.HTTP_400_BAD_REQUEST
return Response({"message": "Brand not found"}, status=status)
return Response({"message": "Product not found"}, status=status_code)