python - 如何将 django 中的 ListCreateAPIView 和 RetrieveUpdateDestroyAPIView 合并到支持所有四种 CRUD 操作的单个 View 中?

标签 python django web django-views django-rest-framework

我想知道如何获得可以处理模型所有操作的单点。为什么 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/

相关文章:

php - SyntaxError : identifier starts immediately after numeric literal. 将 php 变量传递给 JavaScript

python - Python 中的细胞分割和荧光计数

javascript - 将 pandas 数据帧转换为自定义 JSON 格式(然后转换为 JS 对象)

django admin 如何防止暴力攻击?

java - NoSuchMethod 错误有时

networking - 网站无法 ping 通,但可以通过 Web 浏览器打开

python - pyschool 错了吗?

python - 在 Pandas 中有选择地使用 fillna()

javascript - 访问django for循环中的元素

python - Django:手动获取一个URL对应的 View