how to update django database entry based on request.POST data












0














I am working with django on updating my QueryDict into the database. This QueryDict comes from a request.POST method via the html interface.



<QueryDict: {'csrfmiddlewaretoken': ['foo'], 'student_attend': ['Select', 'Select', 'Select'], 'final_student_pk': ['7', '8', '12'], 'submit_student_attendance': ['']}>


What I was attempting to do, was to update my database object student_attend column based on the final_student_pk value. Meaning to say, I was attempting the below:



if 'submit_student_attendance' in request.POST:
to_update = AddNewSchedule.objects.filter(pk=request.POST['final_student_pk'])
to_update.update(student_attend=request.POST['student_attend'])


This does do the job of updating my AddNewSchedule database table. However, it only updates the last pk item. (ie: it only updates item 12 in the database). It does not loop through pk 7 and pk8 to update the database as well.



How can I resolve this?










share|improve this question





























    0














    I am working with django on updating my QueryDict into the database. This QueryDict comes from a request.POST method via the html interface.



    <QueryDict: {'csrfmiddlewaretoken': ['foo'], 'student_attend': ['Select', 'Select', 'Select'], 'final_student_pk': ['7', '8', '12'], 'submit_student_attendance': ['']}>


    What I was attempting to do, was to update my database object student_attend column based on the final_student_pk value. Meaning to say, I was attempting the below:



    if 'submit_student_attendance' in request.POST:
    to_update = AddNewSchedule.objects.filter(pk=request.POST['final_student_pk'])
    to_update.update(student_attend=request.POST['student_attend'])


    This does do the job of updating my AddNewSchedule database table. However, it only updates the last pk item. (ie: it only updates item 12 in the database). It does not loop through pk 7 and pk8 to update the database as well.



    How can I resolve this?










    share|improve this question



























      0












      0








      0







      I am working with django on updating my QueryDict into the database. This QueryDict comes from a request.POST method via the html interface.



      <QueryDict: {'csrfmiddlewaretoken': ['foo'], 'student_attend': ['Select', 'Select', 'Select'], 'final_student_pk': ['7', '8', '12'], 'submit_student_attendance': ['']}>


      What I was attempting to do, was to update my database object student_attend column based on the final_student_pk value. Meaning to say, I was attempting the below:



      if 'submit_student_attendance' in request.POST:
      to_update = AddNewSchedule.objects.filter(pk=request.POST['final_student_pk'])
      to_update.update(student_attend=request.POST['student_attend'])


      This does do the job of updating my AddNewSchedule database table. However, it only updates the last pk item. (ie: it only updates item 12 in the database). It does not loop through pk 7 and pk8 to update the database as well.



      How can I resolve this?










      share|improve this question















      I am working with django on updating my QueryDict into the database. This QueryDict comes from a request.POST method via the html interface.



      <QueryDict: {'csrfmiddlewaretoken': ['foo'], 'student_attend': ['Select', 'Select', 'Select'], 'final_student_pk': ['7', '8', '12'], 'submit_student_attendance': ['']}>


      What I was attempting to do, was to update my database object student_attend column based on the final_student_pk value. Meaning to say, I was attempting the below:



      if 'submit_student_attendance' in request.POST:
      to_update = AddNewSchedule.objects.filter(pk=request.POST['final_student_pk'])
      to_update.update(student_attend=request.POST['student_attend'])


      This does do the job of updating my AddNewSchedule database table. However, it only updates the last pk item. (ie: it only updates item 12 in the database). It does not loop through pk 7 and pk8 to update the database as well.



      How can I resolve this?







      python django






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 3:20









      MD. Khairul Basar

      2,782101938




      2,782101938










      asked Nov 13 '18 at 3:10









      jake wong

      1,26441941




      1,26441941
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Turns out the answer was pretty simple.. All I had to do was to use getlist. I made reference here: https://kite.com/python/docs/django.http.request.QueryDict.



              if 'submit_student_attendance' in request.POST:
          print(request.POST.getlist('student_attend'), request.POST.getlist('final_student_pk'))
          from django.db import transaction
          for key, value in zip(request.POST.getlist('final_student_pk'), request.POST.getlist('student_attend')):
          with transaction.atomic():
          to_update = AddNewSchedule.objects.filter(pk=key)
          to_update.update(student_attend=value)





          share|improve this answer





























            0














            You can not do this in one query because the values are unique for each instance. Try looping through instances and updating one at a time. You can use transaction.atomic to reduce db overhead.



            if 'submit_student_attendance' in request.POST:
            id_list = request.POST.getlist('final_student_pk')
            instance_list = request.POST.getlist('student_attend')

            with transaction.atomic():
            for instance, id in zip(instance_list, id_list):
            to_update = to_update = AddNewSchedule.objects.filter(pk=id)
            to_update.update(student_attend=instance)


            See this answer for more details.






            share|improve this answer























            • Hello, thanks for your answer. but this doesn't seem to solve the problem. it is still not updating the database for all entries.
              – jake wong
              Nov 13 '18 at 6:05










            • Oh. I made a mistake. It should have been getlist().
              – MD. Khairul Basar
              Nov 13 '18 at 6:22










            • Updated my answer.
              – MD. Khairul Basar
              Nov 13 '18 at 6:25











            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%2f53273221%2fhow-to-update-django-database-entry-based-on-request-post-data%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Turns out the answer was pretty simple.. All I had to do was to use getlist. I made reference here: https://kite.com/python/docs/django.http.request.QueryDict.



                if 'submit_student_attendance' in request.POST:
            print(request.POST.getlist('student_attend'), request.POST.getlist('final_student_pk'))
            from django.db import transaction
            for key, value in zip(request.POST.getlist('final_student_pk'), request.POST.getlist('student_attend')):
            with transaction.atomic():
            to_update = AddNewSchedule.objects.filter(pk=key)
            to_update.update(student_attend=value)





            share|improve this answer


























              0














              Turns out the answer was pretty simple.. All I had to do was to use getlist. I made reference here: https://kite.com/python/docs/django.http.request.QueryDict.



                  if 'submit_student_attendance' in request.POST:
              print(request.POST.getlist('student_attend'), request.POST.getlist('final_student_pk'))
              from django.db import transaction
              for key, value in zip(request.POST.getlist('final_student_pk'), request.POST.getlist('student_attend')):
              with transaction.atomic():
              to_update = AddNewSchedule.objects.filter(pk=key)
              to_update.update(student_attend=value)





              share|improve this answer
























                0












                0








                0






                Turns out the answer was pretty simple.. All I had to do was to use getlist. I made reference here: https://kite.com/python/docs/django.http.request.QueryDict.



                    if 'submit_student_attendance' in request.POST:
                print(request.POST.getlist('student_attend'), request.POST.getlist('final_student_pk'))
                from django.db import transaction
                for key, value in zip(request.POST.getlist('final_student_pk'), request.POST.getlist('student_attend')):
                with transaction.atomic():
                to_update = AddNewSchedule.objects.filter(pk=key)
                to_update.update(student_attend=value)





                share|improve this answer












                Turns out the answer was pretty simple.. All I had to do was to use getlist. I made reference here: https://kite.com/python/docs/django.http.request.QueryDict.



                    if 'submit_student_attendance' in request.POST:
                print(request.POST.getlist('student_attend'), request.POST.getlist('final_student_pk'))
                from django.db import transaction
                for key, value in zip(request.POST.getlist('final_student_pk'), request.POST.getlist('student_attend')):
                with transaction.atomic():
                to_update = AddNewSchedule.objects.filter(pk=key)
                to_update.update(student_attend=value)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 6:21









                jake wong

                1,26441941




                1,26441941

























                    0














                    You can not do this in one query because the values are unique for each instance. Try looping through instances and updating one at a time. You can use transaction.atomic to reduce db overhead.



                    if 'submit_student_attendance' in request.POST:
                    id_list = request.POST.getlist('final_student_pk')
                    instance_list = request.POST.getlist('student_attend')

                    with transaction.atomic():
                    for instance, id in zip(instance_list, id_list):
                    to_update = to_update = AddNewSchedule.objects.filter(pk=id)
                    to_update.update(student_attend=instance)


                    See this answer for more details.






                    share|improve this answer























                    • Hello, thanks for your answer. but this doesn't seem to solve the problem. it is still not updating the database for all entries.
                      – jake wong
                      Nov 13 '18 at 6:05










                    • Oh. I made a mistake. It should have been getlist().
                      – MD. Khairul Basar
                      Nov 13 '18 at 6:22










                    • Updated my answer.
                      – MD. Khairul Basar
                      Nov 13 '18 at 6:25
















                    0














                    You can not do this in one query because the values are unique for each instance. Try looping through instances and updating one at a time. You can use transaction.atomic to reduce db overhead.



                    if 'submit_student_attendance' in request.POST:
                    id_list = request.POST.getlist('final_student_pk')
                    instance_list = request.POST.getlist('student_attend')

                    with transaction.atomic():
                    for instance, id in zip(instance_list, id_list):
                    to_update = to_update = AddNewSchedule.objects.filter(pk=id)
                    to_update.update(student_attend=instance)


                    See this answer for more details.






                    share|improve this answer























                    • Hello, thanks for your answer. but this doesn't seem to solve the problem. it is still not updating the database for all entries.
                      – jake wong
                      Nov 13 '18 at 6:05










                    • Oh. I made a mistake. It should have been getlist().
                      – MD. Khairul Basar
                      Nov 13 '18 at 6:22










                    • Updated my answer.
                      – MD. Khairul Basar
                      Nov 13 '18 at 6:25














                    0












                    0








                    0






                    You can not do this in one query because the values are unique for each instance. Try looping through instances and updating one at a time. You can use transaction.atomic to reduce db overhead.



                    if 'submit_student_attendance' in request.POST:
                    id_list = request.POST.getlist('final_student_pk')
                    instance_list = request.POST.getlist('student_attend')

                    with transaction.atomic():
                    for instance, id in zip(instance_list, id_list):
                    to_update = to_update = AddNewSchedule.objects.filter(pk=id)
                    to_update.update(student_attend=instance)


                    See this answer for more details.






                    share|improve this answer














                    You can not do this in one query because the values are unique for each instance. Try looping through instances and updating one at a time. You can use transaction.atomic to reduce db overhead.



                    if 'submit_student_attendance' in request.POST:
                    id_list = request.POST.getlist('final_student_pk')
                    instance_list = request.POST.getlist('student_attend')

                    with transaction.atomic():
                    for instance, id in zip(instance_list, id_list):
                    to_update = to_update = AddNewSchedule.objects.filter(pk=id)
                    to_update.update(student_attend=instance)


                    See this answer for more details.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 13 '18 at 6:23

























                    answered Nov 13 '18 at 3:18









                    MD. Khairul Basar

                    2,782101938




                    2,782101938












                    • Hello, thanks for your answer. but this doesn't seem to solve the problem. it is still not updating the database for all entries.
                      – jake wong
                      Nov 13 '18 at 6:05










                    • Oh. I made a mistake. It should have been getlist().
                      – MD. Khairul Basar
                      Nov 13 '18 at 6:22










                    • Updated my answer.
                      – MD. Khairul Basar
                      Nov 13 '18 at 6:25


















                    • Hello, thanks for your answer. but this doesn't seem to solve the problem. it is still not updating the database for all entries.
                      – jake wong
                      Nov 13 '18 at 6:05










                    • Oh. I made a mistake. It should have been getlist().
                      – MD. Khairul Basar
                      Nov 13 '18 at 6:22










                    • Updated my answer.
                      – MD. Khairul Basar
                      Nov 13 '18 at 6:25
















                    Hello, thanks for your answer. but this doesn't seem to solve the problem. it is still not updating the database for all entries.
                    – jake wong
                    Nov 13 '18 at 6:05




                    Hello, thanks for your answer. but this doesn't seem to solve the problem. it is still not updating the database for all entries.
                    – jake wong
                    Nov 13 '18 at 6:05












                    Oh. I made a mistake. It should have been getlist().
                    – MD. Khairul Basar
                    Nov 13 '18 at 6:22




                    Oh. I made a mistake. It should have been getlist().
                    – MD. Khairul Basar
                    Nov 13 '18 at 6:22












                    Updated my answer.
                    – MD. Khairul Basar
                    Nov 13 '18 at 6:25




                    Updated my answer.
                    – MD. Khairul Basar
                    Nov 13 '18 at 6:25


















                    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%2f53273221%2fhow-to-update-django-database-entry-based-on-request-post-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







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine