Django REST APIフレームワークでのみ更新を許可します
-
21-12-2019 - |
質問
私は、いくつかの分野で1つのモデルしかない非常に単純なアプリケーションに取り組んでいます。これらのモデルは、/管理ページ(標準のDjango管理フレームワークを介して)を介してのみ作成または削除され、REST APIフレームワークがこれらのオブジェクトのみを変更できるようにします。
それを起こす簡単な方法はありますか?
解決
以下のものと同じhttp_method_names
を設定する必要があります。
class WebViewSet(mixins.CreateModelMixin,
mixins.ListModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
model = WebTransaction
http_method_names = ('get', 'put')
. 他のヒント
UPDATE / RETRIEVE MODE MIXENを使用するビューセットを作成します。
from rest_framwork import viewsets, mixins
class FooViewSet(mixens.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
model = Foo
queryset = Foo.objects.all()
serializer_class = FooSerializer
.
これはあなたのモデルのインスタンスを取得または更新するためのApiendポイントするだけです。
If you want just update the objects use UpdateApiView. With this view you will create just the update(PUT Method) for you model.Any doubts follow the documentation in Documentation DRF.
所属していません StackOverflow