我想知道如何获得可以处理模型所有操作的单点。为什么 Django RESTFramework 首先没有可用的泛型?我认为它与其他框架很常见。
class ItemView(ListCreateAPIView, RetrieveUpdateDestroyAPIView):
"""
List all items(GET without id), List single item(GET with id),
create an item(POST) or update an item(PUT with id)
"""
此 View 类应返回普通 GET 请求中的项目列表,如果指定了 id,则返回特定项目。同样,仅当 id 存在时才更新和删除特定项目。
最佳答案
我认为最好的方法是使用 django rest framework viewsets 。例如,您可以定义 ModelViewSet ,它将组合模型的所有 CRUD 方法。
关于python - 如何将 django 中的 ListCreateAPIView 和 RetrieveUpdateDestroyAPIView 合并到支持所有四种 CRUD 操作的单个 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28192455/