django rest framework list update api view
up vote
0
down vote
favorite
I am trying to add update options to the list items. So that if anyone perform 'PATCH' request to it I will get the details and update them. This is my code for the implementation
class SwitchListView(UpdateModelMixin, ListAPIView):
serializer_class = serializers.SwitchSerializer
lookup_field = 'home_id'
def get_queryset(self):
home_id = self.kwargs.get('home_id', None)
if home_id is None or int(home_id) < 0 or
self.request.user.pk != models.Home.objects.filter(pk=home_id)[0].user.pk:
return models.Switch.objects.none()
query = models.Switch.objects.filter(home=models.Home.objects.filter(pk=home_id))
return query
def get(self, request, *args, **kwargs):
return super(SwitchListView, self).get(request, *args, **kwargs)
def partial_update(self, request, *args, **kwargs):
print("Came here")
data = request.data['data']
for i in data:
query = self.get_queryset().filter(i['pk'])
if query.exists():
query.switch_status = i['switch_status']
query.save()
return Response('message': 'successfully updated switch!')
But here the request to the api is only accepting GET, HEAD and OPTIONS. I even tried adding http_method_names = ('get', 'patch') but even this is not working!!
Is there any way to put the patch request to the view ?
Thanks
django django-rest-framework
add a comment |
up vote
0
down vote
favorite
I am trying to add update options to the list items. So that if anyone perform 'PATCH' request to it I will get the details and update them. This is my code for the implementation
class SwitchListView(UpdateModelMixin, ListAPIView):
serializer_class = serializers.SwitchSerializer
lookup_field = 'home_id'
def get_queryset(self):
home_id = self.kwargs.get('home_id', None)
if home_id is None or int(home_id) < 0 or
self.request.user.pk != models.Home.objects.filter(pk=home_id)[0].user.pk:
return models.Switch.objects.none()
query = models.Switch.objects.filter(home=models.Home.objects.filter(pk=home_id))
return query
def get(self, request, *args, **kwargs):
return super(SwitchListView, self).get(request, *args, **kwargs)
def partial_update(self, request, *args, **kwargs):
print("Came here")
data = request.data['data']
for i in data:
query = self.get_queryset().filter(i['pk'])
if query.exists():
query.switch_status = i['switch_status']
query.save()
return Response('message': 'successfully updated switch!')
But here the request to the api is only accepting GET, HEAD and OPTIONS. I even tried adding http_method_names = ('get', 'patch') but even this is not working!!
Is there any way to put the patch request to the view ?
Thanks
django django-rest-framework
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to add update options to the list items. So that if anyone perform 'PATCH' request to it I will get the details and update them. This is my code for the implementation
class SwitchListView(UpdateModelMixin, ListAPIView):
serializer_class = serializers.SwitchSerializer
lookup_field = 'home_id'
def get_queryset(self):
home_id = self.kwargs.get('home_id', None)
if home_id is None or int(home_id) < 0 or
self.request.user.pk != models.Home.objects.filter(pk=home_id)[0].user.pk:
return models.Switch.objects.none()
query = models.Switch.objects.filter(home=models.Home.objects.filter(pk=home_id))
return query
def get(self, request, *args, **kwargs):
return super(SwitchListView, self).get(request, *args, **kwargs)
def partial_update(self, request, *args, **kwargs):
print("Came here")
data = request.data['data']
for i in data:
query = self.get_queryset().filter(i['pk'])
if query.exists():
query.switch_status = i['switch_status']
query.save()
return Response('message': 'successfully updated switch!')
But here the request to the api is only accepting GET, HEAD and OPTIONS. I even tried adding http_method_names = ('get', 'patch') but even this is not working!!
Is there any way to put the patch request to the view ?
Thanks
django django-rest-framework
I am trying to add update options to the list items. So that if anyone perform 'PATCH' request to it I will get the details and update them. This is my code for the implementation
class SwitchListView(UpdateModelMixin, ListAPIView):
serializer_class = serializers.SwitchSerializer
lookup_field = 'home_id'
def get_queryset(self):
home_id = self.kwargs.get('home_id', None)
if home_id is None or int(home_id) < 0 or
self.request.user.pk != models.Home.objects.filter(pk=home_id)[0].user.pk:
return models.Switch.objects.none()
query = models.Switch.objects.filter(home=models.Home.objects.filter(pk=home_id))
return query
def get(self, request, *args, **kwargs):
return super(SwitchListView, self).get(request, *args, **kwargs)
def partial_update(self, request, *args, **kwargs):
print("Came here")
data = request.data['data']
for i in data:
query = self.get_queryset().filter(i['pk'])
if query.exists():
query.switch_status = i['switch_status']
query.save()
return Response('message': 'successfully updated switch!')
But here the request to the api is only accepting GET, HEAD and OPTIONS. I even tried adding http_method_names = ('get', 'patch') but even this is not working!!
Is there any way to put the patch request to the view ?
Thanks
django django-rest-framework
django django-rest-framework
asked Nov 10 at 6:48
rammanoj
103112
103112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
from rest_framework.decorators import detail_route
...
@detail_route(methods=['put', 'patch'])
def partial_update(self, request, *args, **kwargs):
...
try it?
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
from rest_framework.decorators import detail_route
...
@detail_route(methods=['put', 'patch'])
def partial_update(self, request, *args, **kwargs):
...
try it?
add a comment |
up vote
0
down vote
from rest_framework.decorators import detail_route
...
@detail_route(methods=['put', 'patch'])
def partial_update(self, request, *args, **kwargs):
...
try it?
add a comment |
up vote
0
down vote
up vote
0
down vote
from rest_framework.decorators import detail_route
...
@detail_route(methods=['put', 'patch'])
def partial_update(self, request, *args, **kwargs):
...
try it?
from rest_framework.decorators import detail_route
...
@detail_route(methods=['put', 'patch'])
def partial_update(self, request, *args, **kwargs):
...
try it?
answered Nov 10 at 17:31
NerV
23117
23117
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236669%2fdjango-rest-framework-list-update-api-view%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown