在Django REST Framework list_route中使用单一URL进行GET和POST [英] Using a single URL for GET and POST with Django REST Framework list_route
问题描述
在DRF中的ModelViewSets已经非常有用了,但是我试图扩展其中的一个,以便能够以GET请求返回一个对象列表,并在POST请求中处理一个列表。到目前为止,似乎我需要使用 @list_route
装饰器添加这个功能。
我有使用它在其他视图中添加自定义路由很好,但这是我第一次尝试添加一个接受多个方法的路由。这是我到目前为止:
class PickViewset(viewsets.ModelViewSet):
queryset = Pick .objects.all()
serializer_class = PickSerializer
def get_queryset(self):
#gets正确的查询
@list_route(methods = [ 'get','post'])
def update_picks(self,request,league,week,format = None):
如果request.method =='POST':
#process /在这里保存对象
else:
#otherwise返回请求的列表
我想这是有用的,我的问题是在 urls.py
- 这里是相关的东西从那里:
#明确选择方法
/ pre>
update_picks = PickViewset.as_view({'get':'update_picks'}
url(r'^ api / picks /( ?P< league> [\w] +)/(?P< week> [0-9] +)/ $',update_picks,name ='update_picks')
这对GET请求有效,而且如果我将
update_picks
定义更改为update_picks = PickViewset.as_view {'get':'update_picks'}
然后我可以从Viewset 。我需要做什么来让GET和POST请求路由到
update_picks
操作,然后可以使用请求方法进行区分?
我尝试向as_view()添加一个
,{'post':'update_picks'}
,但是不行
我还尝试添加
get_picks = PickViewset.as_view 'get':'update_picks'}
新的URL模式
url(r'^ api / picks /(?P< league> [\w] +)/(?P< week> [0-9] +)/ $',get_picks,name ='get_picks'),
但是没有工作
我查看了单独的
list_routes
与相同的URL,但似乎不被支持,但我可能会错过文档中的某些内容。
感谢任何帮助!
解决方案
动作
参数 ViewSet 是一个dict,所有的方法都在dict中: / p>
get_picks = PickViewset.as_view({
'get':'update_picks',
'post': 'update_picks',
})
The ModelViewSets in DRF have been really helpful, but I'm trying to extend one of them to be able to return a list of objects at a GET request, and process a list on a POST request. So far, it seems like I need to use the
@list_route
decorator to add this functionality.I've used it just fine to add custom routes in other viewsets, however this is the first time I'm trying to add one that accepts more than one method. Here's what I have so far:
class PickViewset(viewsets.ModelViewSet): queryset = Pick.objects.all() serializer_class = PickSerializer def get_queryset(self): #gets the correct queryset @list_route(methods=['get', 'post']) def update_picks(self, request, league, week, format = None): if request.method == 'POST': #process/save objects here else: #otherwise return the requested list
I think this works and that my issue is in
urls.py
- here's the related stuff from there:#bind the pick methods explicitly update_picks = PickViewset.as_view({'get': 'update_picks'}) url(r'^api/picks/(?P<league>[\w ]+)/(?P<week>[0-9]+)/$', update_picks, name='update_picks')
This works fine for GET requests, and if i change the
update_picks
definition toupdate_picks = PickViewset.as_view({'get': 'update_picks'})
then I can step into the POST code from the Viewset. What do I need to do to have both GET and POST requests routed to the
update_picks
action, where they can then be differentiated with the request method?I tried adding a
, {'post': 'update_picks'}
to the as_view(), but that doesn't work.I also tried adding
get_picks = PickViewset.as_view({'get': 'update_picks'})
with new URL pattern
url(r'^api/picks/(?P<league>[\w ]+)/(?P<week>[0-9]+)/$', get_picks, name='get_picks'),
but that didn't work either.
I looked into having separate
list_routes
with the same URL, but that doesn't seem to be supported, though I could have missed something in the docs.Thanks for any help!
解决方案The
actions
argument to theViewSet
is a dict, all methods go in that dict:get_picks = PickViewset.as_view({ 'get': 'update_picks', 'post': 'update_picks', })
这篇关于在Django REST Framework list_route中使用单一URL进行GET和POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!