DRF GnericViewset not accepting json data












1














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



enter image description here



while that of Postman is



enter image description here



How to setup Django to accept json data in PUT?










share|improve this question






















  • 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 with data in update() function.
    – Anuj TBE
    Nov 11 at 7:36
















1














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



enter image description here



while that of Postman is



enter image description here



How to setup Django to accept json data in PUT?










share|improve this question






















  • 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 with data in update() function.
    – Anuj TBE
    Nov 11 at 7:36














1












1








1







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



enter image description here



while that of Postman is



enter image description here



How to setup Django to accept json data in PUT?










share|improve this question













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



enter image description here



while that of Postman is



enter image description here



How to setup Django to accept json data in PUT?







django django-rest-framework






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 6:34









Anuj TBE

1,3691970




1,3691970












  • 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 with data in update() 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










  • 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 with data in update() 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












1 Answer
1






active

oldest

votes


















1














You need to use request.data instead of request.POST in your update method.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You need to use request.data instead of request.POST in your update method.






    share|improve this answer


























      1














      You need to use request.data instead of request.POST in your update method.






      share|improve this answer
























        1












        1








        1






        You need to use request.data instead of request.POST in your update method.






        share|improve this answer












        You need to use request.data instead of request.POST in your update method.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 7:39









        Red Cricket

        4,25983383




        4,25983383






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini