Python rest_framework.generics 模块,ListCreateAPIView() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用rest_framework.generics.ListCreateAPIView()

项目:repeat-aft    作者:ripeta    | 项目源码 | 文件源码
def list_and_crud(model, serializer, queryset=None):
    """
    Create a ListCreateAPIView and RetrieveUpdateDestroyAPIView corresponsing
    to a given model/serializer pair.

    It's like writing the following code manually (when called on model
    ``Foo``)::

        class FooList(generics.ListCreateAPIView):
            queryset = models.Foo.objects.all()
            serializer_class = serializers.FooSerializer


        class FooRUD(generics.RetrieveUpdateDestroyAPIView):
            queryset = models.Foo.objects.all()
            serializer_class = serializers.FooSerializer
    """
    queryset = model.objects.all() if queryset is None else queryset
    lst = type(model.__name__ + "List", (generics.ListCreateAPIView, ), {})
    rud = type(model.__name__ + "CRUD",
               (generics.RetrieveUpdateDestroyAPIView, ), {})
    lst.queryset = queryset
    lst.serializer_class = serializer
    # lst.permission_classes = (permissions.IsAuthenticatedOrReadOnly, )
    rud.queryset = queryset
    rud.serializer_class = serializer
    # rud.permission_classes = (permissions.IsAuthenticatedOrReadOnly, )
    return (lst, rud)