0

我有一个项目需要执行同一模型的不同创建过程。

我们可以通过创建多个ModelViewSet.

class AnimalViewSet(viewsets.ModelViewSet):
    queryset = Animal.objects.all()
    serializer_class = AnimalSerializer

    def create(self, request):
         # just simply create the object...
         pass

端点是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

端点是POST /api/calving/

但我宁愿不这样做。

现在可以create(self, request)在单个中定义多个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

这样我可以避免为同一个模型创建另一个模型视图集。

的端点create(self, request):POST /api/animal/

问题是我不知道calving(self, request):不使用的端点是什么@list_route(methods=['post])

@list_route(methods=['post'])可能会有所帮助,但我想确保有另一种正确的方法来做到这一点。

4

0 回答 0