2014-02-26 32 views
0

我正在一个非常简单的应用程序,只有1个模型与几个字段。我只希望通过/ admin页面(通过标准的Django管理框架)创建或删除这些模型,并允许其余的api框架只改变这些对象。只允许在Django休息api框架更新

有没有简单的方法来实现它?

+0

我不明白你的问题。你想禁用管理中的更新,只允许通过休息框架更新? – fasouto

+0

@fasouto我想禁用在其他框架中创建和删除并启用更新 –

回答

1

只需创建一个使用更新/检索模型混合的视图集。

from rest_framwork import viewsets, mixins 
class FooViewSet(mixens.RetrieveModelMixin, 
       mixins.UpdateModelMixin, 
       viewsets.GenericViewSet): 
    model = Foo 
    queryset = Foo.objects.all() 
    serializer_class = FooSerializer 

这只会给你一个APIEnd点来检索或更新模型的一个实例。

1

你需要设置http_method_names

class WebViewSet(mixins.CreateModelMixin, 
       mixins.ListModelMixin, 
       mixins.UpdateModelMixin, 
       viewsets.GenericViewSet): 

    model = WebTransaction 
    http_method_names = ('get', 'put') 
0

如果你只想更新对象使用UpdateApiView。有了这个视图,您将为model创建更新(PUT方法)。任何怀疑都遵循Documentation DRF中的文档。

+0

欢迎来到SO!只是包含链接并不是一个好的答案。随着时间的推移,链接可能无法访问。请阅读此[如何问](http://stackoverflow.com/help/how-to-ask)以遵循该指南。 – thewaywewere