DRF GnericViewset not accepting json data
up vote
1
down vote
favorite
I'm usig Django 2.x
and Django REST Framework
.
I have a viewset defined like
class UserSettingViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
serializer_class = UserSettingSerializer
permission_classes = (IsAuthenticated, AdminAuthenticationPermission)
def get_object(self):
user_setting = UserSetting.objects.get(user=self.request.user)
return user_setting
def get_queryset(self):
queryset = UserSetting.objects.filter(
user=self.request.user
)
return queryset
def update(self, request, *args, **kwargs):
iso3 = request.POST.get('country', None)
if iso3 is None or not iso3:
raise ValidationError('country': 'Country iso3 is required')
try:
country = Country.objects.get(iso3=iso3.upper())
except ObjectDoesNotExist:
raise ValidationError('country': 'Country does not exists')
user_setting = UserSetting.objects.get(user=request.user)
user_setting.country = country
user_setting.save()
return super(UserSettingViewSet, self).update(request, *args, **kwargs)
When I call update
from Angular
it is not accepting form data and gives the error
'country': 'Country iso3 is required'
While sending data as **form-data from postman is working fine.**
The header of the Angular request is
while that of Postman is
How to setup Django to accept json data in PUT?
django django-rest-framework
|
show 7 more comments
up vote
1
down vote
favorite
I'm usig Django 2.x
and Django REST Framework
.
I have a viewset defined like
class UserSettingViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
serializer_class = UserSettingSerializer
permission_classes = (IsAuthenticated, AdminAuthenticationPermission)
def get_object(self):
user_setting = UserSetting.objects.get(user=self.request.user)
return user_setting
def get_queryset(self):
queryset = UserSetting.objects.filter(
user=self.request.user
)
return queryset
def update(self, request, *args, **kwargs):
iso3 = request.POST.get('country', None)
if iso3 is None or not iso3:
raise ValidationError('country': 'Country iso3 is required')
try:
country = Country.objects.get(iso3=iso3.upper())
except ObjectDoesNotExist:
raise ValidationError('country': 'Country does not exists')
user_setting = UserSetting.objects.get(user=request.user)
user_setting.country = country
user_setting.save()
return super(UserSettingViewSet, self).update(request, *args, **kwargs)
When I call update
from Angular
it is not accepting form data and gives the error
'country': 'Country iso3 is required'
While sending data as **form-data from postman is working fine.**
The header of the Angular request is
while that of Postman is
How to setup Django to accept json data in PUT?
django django-rest-framework
You wouldn't callupdate
from Angular. You would have to make a put request like your curl script does.
– Red Cricket
Nov 11 at 6:46
Yeah, I'm doing the same. Same URL is there in angular.
– Anuj TBE
Nov 11 at 6:48
What does your angular code look like? Can you get post man to send the formdata a json? If so what happens?
– Red Cricket
Nov 11 at 6:54
sending form-data in postman as JSON is giving the same error as that of Angular.
– Anuj TBE
Nov 11 at 6:56
1
Thanks, Got it solved by replacingPOST
withdata
inupdate()
function.
– Anuj TBE
Nov 11 at 7:36
|
show 7 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm usig Django 2.x
and Django REST Framework
.
I have a viewset defined like
class UserSettingViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
serializer_class = UserSettingSerializer
permission_classes = (IsAuthenticated, AdminAuthenticationPermission)
def get_object(self):
user_setting = UserSetting.objects.get(user=self.request.user)
return user_setting
def get_queryset(self):
queryset = UserSetting.objects.filter(
user=self.request.user
)
return queryset
def update(self, request, *args, **kwargs):
iso3 = request.POST.get('country', None)
if iso3 is None or not iso3:
raise ValidationError('country': 'Country iso3 is required')
try:
country = Country.objects.get(iso3=iso3.upper())
except ObjectDoesNotExist:
raise ValidationError('country': 'Country does not exists')
user_setting = UserSetting.objects.get(user=request.user)
user_setting.country = country
user_setting.save()
return super(UserSettingViewSet, self).update(request, *args, **kwargs)
When I call update
from Angular
it is not accepting form data and gives the error
'country': 'Country iso3 is required'
While sending data as **form-data from postman is working fine.**
The header of the Angular request is
while that of Postman is
How to setup Django to accept json data in PUT?
django django-rest-framework
I'm usig Django 2.x
and Django REST Framework
.
I have a viewset defined like
class UserSettingViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
serializer_class = UserSettingSerializer
permission_classes = (IsAuthenticated, AdminAuthenticationPermission)
def get_object(self):
user_setting = UserSetting.objects.get(user=self.request.user)
return user_setting
def get_queryset(self):
queryset = UserSetting.objects.filter(
user=self.request.user
)
return queryset
def update(self, request, *args, **kwargs):
iso3 = request.POST.get('country', None)
if iso3 is None or not iso3:
raise ValidationError('country': 'Country iso3 is required')
try:
country = Country.objects.get(iso3=iso3.upper())
except ObjectDoesNotExist:
raise ValidationError('country': 'Country does not exists')
user_setting = UserSetting.objects.get(user=request.user)
user_setting.country = country
user_setting.save()
return super(UserSettingViewSet, self).update(request, *args, **kwargs)
When I call update
from Angular
it is not accepting form data and gives the error
'country': 'Country iso3 is required'
While sending data as **form-data from postman is working fine.**
The header of the Angular request is
while that of Postman is
How to setup Django to accept json data in PUT?
django django-rest-framework
django django-rest-framework
asked Nov 11 at 6:34
Anuj TBE
1,3401967
1,3401967
You wouldn't callupdate
from Angular. You would have to make a put request like your curl script does.
– Red Cricket
Nov 11 at 6:46
Yeah, I'm doing the same. Same URL is there in angular.
– Anuj TBE
Nov 11 at 6:48
What does your angular code look like? Can you get post man to send the formdata a json? If so what happens?
– Red Cricket
Nov 11 at 6:54
sending form-data in postman as JSON is giving the same error as that of Angular.
– Anuj TBE
Nov 11 at 6:56
1
Thanks, Got it solved by replacingPOST
withdata
inupdate()
function.
– Anuj TBE
Nov 11 at 7:36
|
show 7 more comments
You wouldn't callupdate
from Angular. You would have to make a put request like your curl script does.
– Red Cricket
Nov 11 at 6:46
Yeah, I'm doing the same. Same URL is there in angular.
– Anuj TBE
Nov 11 at 6:48
What does your angular code look like? Can you get post man to send the formdata a json? If so what happens?
– Red Cricket
Nov 11 at 6:54
sending form-data in postman as JSON is giving the same error as that of Angular.
– Anuj TBE
Nov 11 at 6:56
1
Thanks, Got it solved by replacingPOST
withdata
inupdate()
function.
– Anuj TBE
Nov 11 at 7:36
You wouldn't call
update
from Angular. You would have to make a put request like your curl script does.– Red Cricket
Nov 11 at 6:46
You wouldn't call
update
from Angular. You would have to make a put request like your curl script does.– Red Cricket
Nov 11 at 6:46
Yeah, I'm doing the same. Same URL is there in angular.
– Anuj TBE
Nov 11 at 6:48
Yeah, I'm doing the same. Same URL is there in angular.
– Anuj TBE
Nov 11 at 6:48
What does your angular code look like? Can you get post man to send the formdata a json? If so what happens?
– Red Cricket
Nov 11 at 6:54
What does your angular code look like? Can you get post man to send the formdata a json? If so what happens?
– Red Cricket
Nov 11 at 6:54
sending form-data in postman as JSON is giving the same error as that of Angular.
– Anuj TBE
Nov 11 at 6:56
sending form-data in postman as JSON is giving the same error as that of Angular.
– Anuj TBE
Nov 11 at 6:56
1
1
Thanks, Got it solved by replacing
POST
with data
in update()
function.– Anuj TBE
Nov 11 at 7:36
Thanks, Got it solved by replacing
POST
with data
in update()
function.– Anuj TBE
Nov 11 at 7:36
|
show 7 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You need to use request.data
instead of request.POST
in your update method.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need to use request.data
instead of request.POST
in your update method.
add a comment |
up vote
1
down vote
accepted
You need to use request.data
instead of request.POST
in your update method.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need to use request.data
instead of request.POST
in your update method.
You need to use request.data
instead of request.POST
in your update method.
answered Nov 11 at 7:39
Red Cricket
4,09183381
4,09183381
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%2f53246425%2fdrf-gnericviewset-not-accepting-json-data%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
You wouldn't call
update
from Angular. You would have to make a put request like your curl script does.– Red Cricket
Nov 11 at 6:46
Yeah, I'm doing the same. Same URL is there in angular.
– Anuj TBE
Nov 11 at 6:48
What does your angular code look like? Can you get post man to send the formdata a json? If so what happens?
– Red Cricket
Nov 11 at 6:54
sending form-data in postman as JSON is giving the same error as that of Angular.
– Anuj TBE
Nov 11 at 6:56
1
Thanks, Got it solved by replacing
POST
withdata
inupdate()
function.– Anuj TBE
Nov 11 at 7:36