Function python for odoo 10











up vote
-2
down vote

favorite












I have a function for filling in many fields automatically with an api.onchange() decorator.



@api.onchange('nursery_morning', 'nursery_evening', 'responsible_partner')
def retrieve_responsible_nursery(self):
if self.nursery_morning or self.nursery_evening:
if self.responsible_partner:
if not self.resp_civility1 and not self.resp_name1 and not self.resp_cp1 and not self.resp_num1
and not self.resp_address1 and not self.resp_town1
and not self.resp_phone1 and not self.resp_phonemobile1:
self.resp_civility1 = self.responsible_partner.title.name
self.resp_name1 = self.responsible_partner.name
self.resp_cp1 = self.responsible_partner.zip_id.name
self.resp_num1 = self.responsible_partner.street_number_id.name
self.resp_address1 = self.responsible_partner.street_id.name
self.resp_town1 = self.responsible_partner.city_id.name
self.resp_phone1 = self.responsible_partner.phone
self.resp_phonemobile1 = self.responsible_partner.mobile


This function works, but I do not want the fields to change until the fields are saved in the database and not before.



Currently, the fields do not change once one of the fields listed are filled but not saved in database










share|improve this question




























    up vote
    -2
    down vote

    favorite












    I have a function for filling in many fields automatically with an api.onchange() decorator.



    @api.onchange('nursery_morning', 'nursery_evening', 'responsible_partner')
    def retrieve_responsible_nursery(self):
    if self.nursery_morning or self.nursery_evening:
    if self.responsible_partner:
    if not self.resp_civility1 and not self.resp_name1 and not self.resp_cp1 and not self.resp_num1
    and not self.resp_address1 and not self.resp_town1
    and not self.resp_phone1 and not self.resp_phonemobile1:
    self.resp_civility1 = self.responsible_partner.title.name
    self.resp_name1 = self.responsible_partner.name
    self.resp_cp1 = self.responsible_partner.zip_id.name
    self.resp_num1 = self.responsible_partner.street_number_id.name
    self.resp_address1 = self.responsible_partner.street_id.name
    self.resp_town1 = self.responsible_partner.city_id.name
    self.resp_phone1 = self.responsible_partner.phone
    self.resp_phonemobile1 = self.responsible_partner.mobile


    This function works, but I do not want the fields to change until the fields are saved in the database and not before.



    Currently, the fields do not change once one of the fields listed are filled but not saved in database










    share|improve this question


























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I have a function for filling in many fields automatically with an api.onchange() decorator.



      @api.onchange('nursery_morning', 'nursery_evening', 'responsible_partner')
      def retrieve_responsible_nursery(self):
      if self.nursery_morning or self.nursery_evening:
      if self.responsible_partner:
      if not self.resp_civility1 and not self.resp_name1 and not self.resp_cp1 and not self.resp_num1
      and not self.resp_address1 and not self.resp_town1
      and not self.resp_phone1 and not self.resp_phonemobile1:
      self.resp_civility1 = self.responsible_partner.title.name
      self.resp_name1 = self.responsible_partner.name
      self.resp_cp1 = self.responsible_partner.zip_id.name
      self.resp_num1 = self.responsible_partner.street_number_id.name
      self.resp_address1 = self.responsible_partner.street_id.name
      self.resp_town1 = self.responsible_partner.city_id.name
      self.resp_phone1 = self.responsible_partner.phone
      self.resp_phonemobile1 = self.responsible_partner.mobile


      This function works, but I do not want the fields to change until the fields are saved in the database and not before.



      Currently, the fields do not change once one of the fields listed are filled but not saved in database










      share|improve this question















      I have a function for filling in many fields automatically with an api.onchange() decorator.



      @api.onchange('nursery_morning', 'nursery_evening', 'responsible_partner')
      def retrieve_responsible_nursery(self):
      if self.nursery_morning or self.nursery_evening:
      if self.responsible_partner:
      if not self.resp_civility1 and not self.resp_name1 and not self.resp_cp1 and not self.resp_num1
      and not self.resp_address1 and not self.resp_town1
      and not self.resp_phone1 and not self.resp_phonemobile1:
      self.resp_civility1 = self.responsible_partner.title.name
      self.resp_name1 = self.responsible_partner.name
      self.resp_cp1 = self.responsible_partner.zip_id.name
      self.resp_num1 = self.responsible_partner.street_number_id.name
      self.resp_address1 = self.responsible_partner.street_id.name
      self.resp_town1 = self.responsible_partner.city_id.name
      self.resp_phone1 = self.responsible_partner.phone
      self.resp_phonemobile1 = self.responsible_partner.mobile


      This function works, but I do not want the fields to change until the fields are saved in the database and not before.



      Currently, the fields do not change once one of the fields listed are filled but not saved in database







      python odoo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 10:21









      John Murray

      591414




      591414










      asked Nov 8 at 9:25









      PseudoWithK

      39




      39
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Two ways you can approach this, it seems that you could change these fields to be related fields if they are only ever going to hold the values from a related record (responsible_partner).



          resp_civility1 = fields.Char("Field Label", related="responsible_partner.title.name")


          Editing this field will edit the linked record as well, so it will not be a good fit if you want to change this value at all, or you could set it as read-only if you don't want it to be edited from here.



          Another way to do it would be to override the write method, this will write the values at the time of saving the record and would look like this:



          @api.multi
          def write(self, vals):
          res = super(ModelName, self).write(vals)

          if vals.get('nursery_morning') or vals.get('nursery_evening') or vals.get('responsible_partner'):
          if res.nursery_morning or res.nursery_evening:
          if res.responsible_partner:
          if not res.resp_civility1 and not res.resp_name1 and not res.resp_cp1 and not res.resp_num1
          and not res.resp_address1 and not res.resp_town1
          and not res.resp_phone1 and not res.resp_phonemobile1:
          res.resp_civility1 = res.responsible_partner.title.name
          res.resp_name1 = res.responsible_partner.name
          res.resp_cp1 = res.responsible_partner.zip_id.name
          res.resp_num1 = res.responsible_partner.street_number_id.name
          res.resp_address1 = res.responsible_partner.street_id.name
          res.resp_town1 = res.responsible_partner.city_id.name
          res.resp_phone1 = res.responsible_partner.phone
          res.resp_phonemobile1 = res.responsible_partner.mobile
          return res


          This will first call the write method of the model, then it will check if the three fields are being written to, if so it will run the same logic from your onchange method. Replace ModelName with whatever you have named your model class.






          share|improve this answer








          New contributor




          djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















            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',
            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%2f53204774%2ffunction-python-for-odoo-10%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Two ways you can approach this, it seems that you could change these fields to be related fields if they are only ever going to hold the values from a related record (responsible_partner).



            resp_civility1 = fields.Char("Field Label", related="responsible_partner.title.name")


            Editing this field will edit the linked record as well, so it will not be a good fit if you want to change this value at all, or you could set it as read-only if you don't want it to be edited from here.



            Another way to do it would be to override the write method, this will write the values at the time of saving the record and would look like this:



            @api.multi
            def write(self, vals):
            res = super(ModelName, self).write(vals)

            if vals.get('nursery_morning') or vals.get('nursery_evening') or vals.get('responsible_partner'):
            if res.nursery_morning or res.nursery_evening:
            if res.responsible_partner:
            if not res.resp_civility1 and not res.resp_name1 and not res.resp_cp1 and not res.resp_num1
            and not res.resp_address1 and not res.resp_town1
            and not res.resp_phone1 and not res.resp_phonemobile1:
            res.resp_civility1 = res.responsible_partner.title.name
            res.resp_name1 = res.responsible_partner.name
            res.resp_cp1 = res.responsible_partner.zip_id.name
            res.resp_num1 = res.responsible_partner.street_number_id.name
            res.resp_address1 = res.responsible_partner.street_id.name
            res.resp_town1 = res.responsible_partner.city_id.name
            res.resp_phone1 = res.responsible_partner.phone
            res.resp_phonemobile1 = res.responsible_partner.mobile
            return res


            This will first call the write method of the model, then it will check if the three fields are being written to, if so it will run the same logic from your onchange method. Replace ModelName with whatever you have named your model class.






            share|improve this answer








            New contributor




            djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






















              up vote
              0
              down vote













              Two ways you can approach this, it seems that you could change these fields to be related fields if they are only ever going to hold the values from a related record (responsible_partner).



              resp_civility1 = fields.Char("Field Label", related="responsible_partner.title.name")


              Editing this field will edit the linked record as well, so it will not be a good fit if you want to change this value at all, or you could set it as read-only if you don't want it to be edited from here.



              Another way to do it would be to override the write method, this will write the values at the time of saving the record and would look like this:



              @api.multi
              def write(self, vals):
              res = super(ModelName, self).write(vals)

              if vals.get('nursery_morning') or vals.get('nursery_evening') or vals.get('responsible_partner'):
              if res.nursery_morning or res.nursery_evening:
              if res.responsible_partner:
              if not res.resp_civility1 and not res.resp_name1 and not res.resp_cp1 and not res.resp_num1
              and not res.resp_address1 and not res.resp_town1
              and not res.resp_phone1 and not res.resp_phonemobile1:
              res.resp_civility1 = res.responsible_partner.title.name
              res.resp_name1 = res.responsible_partner.name
              res.resp_cp1 = res.responsible_partner.zip_id.name
              res.resp_num1 = res.responsible_partner.street_number_id.name
              res.resp_address1 = res.responsible_partner.street_id.name
              res.resp_town1 = res.responsible_partner.city_id.name
              res.resp_phone1 = res.responsible_partner.phone
              res.resp_phonemobile1 = res.responsible_partner.mobile
              return res


              This will first call the write method of the model, then it will check if the three fields are being written to, if so it will run the same logic from your onchange method. Replace ModelName with whatever you have named your model class.






              share|improve this answer








              New contributor




              djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.




















                up vote
                0
                down vote










                up vote
                0
                down vote









                Two ways you can approach this, it seems that you could change these fields to be related fields if they are only ever going to hold the values from a related record (responsible_partner).



                resp_civility1 = fields.Char("Field Label", related="responsible_partner.title.name")


                Editing this field will edit the linked record as well, so it will not be a good fit if you want to change this value at all, or you could set it as read-only if you don't want it to be edited from here.



                Another way to do it would be to override the write method, this will write the values at the time of saving the record and would look like this:



                @api.multi
                def write(self, vals):
                res = super(ModelName, self).write(vals)

                if vals.get('nursery_morning') or vals.get('nursery_evening') or vals.get('responsible_partner'):
                if res.nursery_morning or res.nursery_evening:
                if res.responsible_partner:
                if not res.resp_civility1 and not res.resp_name1 and not res.resp_cp1 and not res.resp_num1
                and not res.resp_address1 and not res.resp_town1
                and not res.resp_phone1 and not res.resp_phonemobile1:
                res.resp_civility1 = res.responsible_partner.title.name
                res.resp_name1 = res.responsible_partner.name
                res.resp_cp1 = res.responsible_partner.zip_id.name
                res.resp_num1 = res.responsible_partner.street_number_id.name
                res.resp_address1 = res.responsible_partner.street_id.name
                res.resp_town1 = res.responsible_partner.city_id.name
                res.resp_phone1 = res.responsible_partner.phone
                res.resp_phonemobile1 = res.responsible_partner.mobile
                return res


                This will first call the write method of the model, then it will check if the three fields are being written to, if so it will run the same logic from your onchange method. Replace ModelName with whatever you have named your model class.






                share|improve this answer








                New contributor




                djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                Two ways you can approach this, it seems that you could change these fields to be related fields if they are only ever going to hold the values from a related record (responsible_partner).



                resp_civility1 = fields.Char("Field Label", related="responsible_partner.title.name")


                Editing this field will edit the linked record as well, so it will not be a good fit if you want to change this value at all, or you could set it as read-only if you don't want it to be edited from here.



                Another way to do it would be to override the write method, this will write the values at the time of saving the record and would look like this:



                @api.multi
                def write(self, vals):
                res = super(ModelName, self).write(vals)

                if vals.get('nursery_morning') or vals.get('nursery_evening') or vals.get('responsible_partner'):
                if res.nursery_morning or res.nursery_evening:
                if res.responsible_partner:
                if not res.resp_civility1 and not res.resp_name1 and not res.resp_cp1 and not res.resp_num1
                and not res.resp_address1 and not res.resp_town1
                and not res.resp_phone1 and not res.resp_phonemobile1:
                res.resp_civility1 = res.responsible_partner.title.name
                res.resp_name1 = res.responsible_partner.name
                res.resp_cp1 = res.responsible_partner.zip_id.name
                res.resp_num1 = res.responsible_partner.street_number_id.name
                res.resp_address1 = res.responsible_partner.street_id.name
                res.resp_town1 = res.responsible_partner.city_id.name
                res.resp_phone1 = res.responsible_partner.phone
                res.resp_phonemobile1 = res.responsible_partner.mobile
                return res


                This will first call the write method of the model, then it will check if the three fields are being written to, if so it will run the same logic from your onchange method. Replace ModelName with whatever you have named your model class.







                share|improve this answer








                New contributor




                djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered Nov 9 at 13:28









                djames

                161




                161




                New contributor




                djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                djames is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204774%2ffunction-python-for-odoo-10%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)