Django Rest 框架增删改查

Django Rest Framework CRUD

我正在使用 Django Rest Framework 构建一个 API,我想知道仅使用 ModelViewSet class 是否足以实现 CRUD。我担心的是前端消费和使用创建、读取、更新和删除功能是不够的。

简短回答“是”。

但是,我建议您阅读 DRF 的官方文档。

ModelViewSet的 docs

The ModelViewSet class inherits from GenericAPIView and includes implementations for various actions, by mixing in the behavior of the various mixin classes.

The actions provided by the ModelViewSet class are .list(), .retrieve(), .create(), .update(), .partial_update(), and .destroy().

GenericAPIView 的 docs

This class extends REST framework's APIView class, adding commonly required behavior for standard list and detail views.

Each of the concrete generic views provided is built by combining GenericAPIView, with one or more mixin classes.

--

还有很多博客解释为什么以及何时使用这些 class。

Django Rest Framework ViewSets

--

最后,我和你一样都是社区的新手。我不确定这里是否允许此类问题。但是,我想说的是..

Stop worrying, just go and try out yourself. I believe that people in the community would willing to help you out if you've stuck.