Adding live_stocks Action To HerdViewSet
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
|
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
|
||||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||||
from apps.authentication.api.v1.api import UserViewSet
|
from apps.authentication.api.v1.api import UserViewSet
|
||||||
|
from apps.livestock.web.api.v1.serializers import LiveStockSerializer
|
||||||
from common.helpers import get_organization_by_user
|
from common.helpers import get_organization_by_user
|
||||||
from rest_framework.exceptions import APIException
|
from rest_framework.exceptions import APIException
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
@@ -94,6 +95,25 @@ class HerdViewSet(viewsets.ModelViewSet):
|
|||||||
except APIException as e:
|
except APIException as e:
|
||||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=True,
|
||||||
|
url_path='live_stocks',
|
||||||
|
url_name='live_stocks',
|
||||||
|
name='live_stocks'
|
||||||
|
)
|
||||||
|
def live_stocks(self, request, pk=None):
|
||||||
|
""" list of herd live_stocks"""
|
||||||
|
|
||||||
|
herd = self.get_object()
|
||||||
|
queryset = herd.live_stock_herd.all() # get herd live_stocks
|
||||||
|
|
||||||
|
# paginate queryset
|
||||||
|
page = self.paginate_queryset(queryset)
|
||||||
|
if page is not None:
|
||||||
|
serializer = LiveStockSerializer(page, many=True)
|
||||||
|
return self.get_paginated_response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin):
|
||||||
queryset = Rancher.objects.all()
|
queryset = Rancher.objects.all()
|
||||||
|
|||||||
Reference in New Issue
Block a user