From 5f80979aa23645aee179158233f70cae895793db Mon Sep 17 00:00:00 2001 From: Ian Andwati Date: Sun, 3 Mar 2024 11:57:08 +0300 Subject: [PATCH] model changes --- .idea/store.iml | 2 +- .idea/workspace.xml | 57 ++++++++++++++++++++++++++--------------- products/models.py | 19 -------------- products/serializers.py | 14 +--------- products/urls.py | 4 +-- products/views.py | 44 +++++++++++++++---------------- 6 files changed, 62 insertions(+), 78 deletions(-) diff --git a/.idea/store.iml b/.idea/store.iml index ded8cca..e9d4aae 100644 --- a/.idea/store.iml +++ b/.idea/store.iml @@ -14,7 +14,7 @@ - + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2bf8ba6..6a6ba11 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,12 +5,12 @@ - - + - + + + - - { - "keyToString": { - "ASKED_ADD_EXTERNAL_FILES": "true", - "Django Server.store.executor": "Run", - "RunOnceActivity.OpenDjangoStructureViewOnStart": "true", - "RunOnceActivity.OpenProjectViewOnStart": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "git-widget-placeholder": "main", - "ignore.virus.scanning.warn.message": "true", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "vue.rearranger.settings.migration": "true" + +}]]> @@ -100,7 +104,8 @@ + + + + + file://$PROJECT_DIR$/products/serializers.py + + + + diff --git a/products/models.py b/products/models.py index bf4740c..0532880 100644 --- a/products/models.py +++ b/products/models.py @@ -1,23 +1,6 @@ from django.db import models -class Brand(models.Model): - name = models.CharField(max_length=100) - description = models.TextField(blank=True) - - def __str__(self): - return f"{self.name}" - - -class Supplier(models.Model): - name = models.CharField(max_length=100) - email = models.EmailField() - phone = models.CharField(max_length=20) - - def __str__(self): - return f"{self.name}" - - class Product(models.Model): name = models.CharField(max_length=100) description = models.TextField() @@ -25,8 +8,6 @@ class Product(models.Model): price = models.DecimalField(max_digits=10, decimal_places=2) quantity = models.IntegerField(default=0) barcode = models.CharField(max_length=100, blank=True, null=True) - brand = models.ForeignKey(Brand, models.DO_NOTHING) - supplier = models.ForeignKey(Supplier, models.DO_NOTHING) def __str__(self): return f"{self.name}" diff --git a/products/serializers.py b/products/serializers.py index 21684ec..fe87668 100644 --- a/products/serializers.py +++ b/products/serializers.py @@ -1,18 +1,6 @@ from rest_framework import serializers -from .models import Brand, Product, Supplier - - -class BrandSerializer(serializers.ModelSerializer): - class Meta: - model = Brand - fields = "__all__" - - -class SupplierSerializer(serializers.ModelSerializer): - class Meta: - model = Supplier - fields = "__all__" +from .models import Product class ProductSerializer(serializers.ModelSerializer): diff --git a/products/urls.py b/products/urls.py index 473ea04..0541d78 100644 --- a/products/urls.py +++ b/products/urls.py @@ -1,8 +1,8 @@ from django.urls import include, path from rest_framework.routers import DefaultRouter -from .views import BrandViewSet +from .views import ProductViewSet router = DefaultRouter() -router.register(r"Brands", BrandViewSet) +router.register(r"Products", ProductViewSet) urlpatterns = router.urls diff --git a/products/views.py b/products/views.py index e358358..64f1f26 100644 --- a/products/views.py +++ b/products/views.py @@ -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)