Cannot set/read Django simple history change reason










2















I'm using the simple_history package for Django and would like to record whether the user creates, updates or deletes a model object. I thought I'd use the history change reason for that purpose and in models.py do something like



def save(...):
if not self.id:
self.id = uuid()
self.changeReason = 'create'
else:
self.changeReason = 'update'
super(MyModel, self).save(...)


When I save the model this 'changeReason' field seems to be set (at least there are no errors) but when I try to read it in a test case like .history.first().instance.changeReason it complains that the field 'changeReason' does not exist. The history.first() call works so I do have a history record.



I have SIMPLE_HISTORY_HISTORY_CHANGE_REASON_USE_TEXT_FIELD = True in settings.py



I'm probably overlooking something basic but I can't seem to figure out what...



Thanks,










share|improve this question


























    2















    I'm using the simple_history package for Django and would like to record whether the user creates, updates or deletes a model object. I thought I'd use the history change reason for that purpose and in models.py do something like



    def save(...):
    if not self.id:
    self.id = uuid()
    self.changeReason = 'create'
    else:
    self.changeReason = 'update'
    super(MyModel, self).save(...)


    When I save the model this 'changeReason' field seems to be set (at least there are no errors) but when I try to read it in a test case like .history.first().instance.changeReason it complains that the field 'changeReason' does not exist. The history.first() call works so I do have a history record.



    I have SIMPLE_HISTORY_HISTORY_CHANGE_REASON_USE_TEXT_FIELD = True in settings.py



    I'm probably overlooking something basic but I can't seem to figure out what...



    Thanks,










    share|improve this question
























      2












      2








      2








      I'm using the simple_history package for Django and would like to record whether the user creates, updates or deletes a model object. I thought I'd use the history change reason for that purpose and in models.py do something like



      def save(...):
      if not self.id:
      self.id = uuid()
      self.changeReason = 'create'
      else:
      self.changeReason = 'update'
      super(MyModel, self).save(...)


      When I save the model this 'changeReason' field seems to be set (at least there are no errors) but when I try to read it in a test case like .history.first().instance.changeReason it complains that the field 'changeReason' does not exist. The history.first() call works so I do have a history record.



      I have SIMPLE_HISTORY_HISTORY_CHANGE_REASON_USE_TEXT_FIELD = True in settings.py



      I'm probably overlooking something basic but I can't seem to figure out what...



      Thanks,










      share|improve this question














      I'm using the simple_history package for Django and would like to record whether the user creates, updates or deletes a model object. I thought I'd use the history change reason for that purpose and in models.py do something like



      def save(...):
      if not self.id:
      self.id = uuid()
      self.changeReason = 'create'
      else:
      self.changeReason = 'update'
      super(MyModel, self).save(...)


      When I save the model this 'changeReason' field seems to be set (at least there are no errors) but when I try to read it in a test case like .history.first().instance.changeReason it complains that the field 'changeReason' does not exist. The history.first() call works so I do have a history record.



      I have SIMPLE_HISTORY_HISTORY_CHANGE_REASON_USE_TEXT_FIELD = True in settings.py



      I'm probably overlooking something basic but I can't seem to figure out what...



      Thanks,







      django-simple-history






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 21:23









      RalphRalph

      456




      456






















          2 Answers
          2






          active

          oldest

          votes


















          0














          My bad...
          Setting self.changeReason = 'create' in the model is the right thing to do. The problem was incorrect checking of the change reason in my test case.



          I checked



          self.assertEqual(<object>.history.first().instance.changeReason, 'create')


          but it should be



          self.assertEqual(<object>.history.first().history_change_reason, 'create')


          That passed the test :)






          share|improve this answer






























            0














            The above answer is correct for checking the changeReason; however, whether or not a record was created, updated, or deleted is already tracked in the history table by default in the history_type field.






            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%2f53328089%2fcannot-set-read-django-simple-history-change-reason%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














              My bad...
              Setting self.changeReason = 'create' in the model is the right thing to do. The problem was incorrect checking of the change reason in my test case.



              I checked



              self.assertEqual(<object>.history.first().instance.changeReason, 'create')


              but it should be



              self.assertEqual(<object>.history.first().history_change_reason, 'create')


              That passed the test :)






              share|improve this answer



























                0














                My bad...
                Setting self.changeReason = 'create' in the model is the right thing to do. The problem was incorrect checking of the change reason in my test case.



                I checked



                self.assertEqual(<object>.history.first().instance.changeReason, 'create')


                but it should be



                self.assertEqual(<object>.history.first().history_change_reason, 'create')


                That passed the test :)






                share|improve this answer

























                  0












                  0








                  0







                  My bad...
                  Setting self.changeReason = 'create' in the model is the right thing to do. The problem was incorrect checking of the change reason in my test case.



                  I checked



                  self.assertEqual(<object>.history.first().instance.changeReason, 'create')


                  but it should be



                  self.assertEqual(<object>.history.first().history_change_reason, 'create')


                  That passed the test :)






                  share|improve this answer













                  My bad...
                  Setting self.changeReason = 'create' in the model is the right thing to do. The problem was incorrect checking of the change reason in my test case.



                  I checked



                  self.assertEqual(<object>.history.first().instance.changeReason, 'create')


                  but it should be



                  self.assertEqual(<object>.history.first().history_change_reason, 'create')


                  That passed the test :)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 21:31









                  RalphRalph

                  456




                  456























                      0














                      The above answer is correct for checking the changeReason; however, whether or not a record was created, updated, or deleted is already tracked in the history table by default in the history_type field.






                      share|improve this answer



























                        0














                        The above answer is correct for checking the changeReason; however, whether or not a record was created, updated, or deleted is already tracked in the history table by default in the history_type field.






                        share|improve this answer

























                          0












                          0








                          0







                          The above answer is correct for checking the changeReason; however, whether or not a record was created, updated, or deleted is already tracked in the history table by default in the history_type field.






                          share|improve this answer













                          The above answer is correct for checking the changeReason; however, whether or not a record was created, updated, or deleted is already tracked in the history table by default in the history_type field.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 12 '18 at 19:56









                          Ross MechanicRoss Mechanic

                          5113




                          5113



























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328089%2fcannot-set-read-django-simple-history-change-reason%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

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              政党

                              天津地下鉄3号線