I have a project which need to perform different create processes of the same Model.
We can achieved this one by creating multiple ModelViewSet
.
class AnimalViewSet(viewsets.ModelViewSet):
queryset = Animal.objects.all()
serializer_class = AnimalSerializer
def create(self, request):
# just simply create the object...
pass
Endpoint is POST /api/animal/
class CalvingViewSet(viewsets.ModelViewSet):
# This is called when an animal has successfully delivered its new born..
queryset = Animal.objects.all()
serializer_class = AnimalSerializer
def create(self, request):
# define parent animals pregnancy as success
# and more..
# finally create the object..
pass
Endpoint is POST /api/calving/
But i prefer not doing so.
Now it is possible to define multiple create(self, request)
in a single ModelViewSet
?
class AnimalViewSet(viewsets.ModelViewSet):
queryset = Animal.objects.all()
serializer_class = AnimalSerializer
def create(self, request):
# just simply create the object...
pass
def calving(self, request):
# create the object..
# define parent animals pregnancy as success
# and more..
pass
By this i can avoid creating another modelviewset for the same Model.
Endpoint for create(self, request):
is POST /api/animal/
.
The problem is i don't have any idea what is the endpoint for calving(self, request):
without using @list_route(methods=['post])
@list_route(methods=['post'])
may help but i want to make sure there is another right way to do this.