Added least stocked product url

This commit is contained in:
keannu125 2023-03-07 19:28:38 +08:00
parent 78c9a67f07
commit 3205880bde
3 changed files with 19 additions and 3 deletions

View file

@ -1,4 +1,5 @@
from rest_framework import serializers, mixins from rest_framework import serializers, mixins
from django.contrib.auth.models import User
from simple_history.models import HistoricalRecords from simple_history.models import HistoricalRecords
from .models import Product from .models import Product
@ -28,5 +29,7 @@ class LogSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = Product.history.model model = Product.history.model
fields = ('history_id', 'name', 'quantity', 'history_date') fields = ('history_id', 'name', 'quantity',
read_only_fields = ('history_id', 'name', 'quantity', 'history_date') 'history_date', 'history_user_id')
read_only_fields = ('history_id', 'name', 'quantity',
'history_date', 'history_user_id')

View file

@ -5,6 +5,7 @@ from . import views
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'products', views.ProductViewSet) router.register(r'products', views.ProductViewSet)
router.register(r'logs', views.LogViewSet) router.register(r'logs', views.LogViewSet)
router.register(r'lowest_stock_product', views.LeastStockProductViewSet)
# Wire up our API using automatic URL routing. # Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API. # Additionally, we include login URLs for the browsable API.

View file

@ -1,5 +1,6 @@
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets from rest_framework import viewsets
from django.db.models import Min
from .serializers import ProductSerializer, LogSerializer from .serializers import ProductSerializer, LogSerializer
from .models import Product from .models import Product
@ -7,7 +8,18 @@ from .models import Product
class ProductViewSet(viewsets.ModelViewSet): class ProductViewSet(viewsets.ModelViewSet):
# permission_classes = [IsAuthenticated] # permission_classes = [IsAuthenticated]
serializer_class = ProductSerializer serializer_class = ProductSerializer
queryset = Product.objects.all() queryset = Product.objects.all().order_by('-date_added')
class LeastStockProductViewSet(viewsets.ModelViewSet):
# permission_classes = [IsAuthenticated]
http_method_names = ['get']
serializer_class = ProductSerializer
queryset = Product.objects.all().order_by('quantity')
def get_queryset(self):
return super().get_queryset()[:1]
# queryset = Product.objects.all().order_by('quantity').first()
class LogViewSet(viewsets.ModelViewSet): class LogViewSet(viewsets.ModelViewSet):