Django post_save signal called before save done2019 Community Moderator ElectionDjango's post_save signal behaves weirdly with models using multi-table inheritanceDjango - post_save timing issue - how to implement a signal for action “database save finalized”Is there any difference in the use of the signals post_delete and post_save?Reading a recursive field on post_save of django modelDjango assert post_save signal calledhandle `post_save` signal in celeryDjango - Multiple post_save signals after create despite dispatch_uidDjango-polymorphic pre-save signaldjango : How to use signals?How to check if Django Signal works?

What are these green text/line displays shown during the livestream of Crew Dragon's approach to dock with the ISS?

Pure Functions: Does "No Side Effects" Imply "Always Same Output, Given Same Input"?

Using std::set container for range items

Are paired adjectives bad style?

What is a term for a function that when called repeatedly, has the same effect as calling once?

When was drinking water recognized as crucial in marathon running?

Canadian citizen, on US no-fly list. What can I do in order to be allowed on flights which go through US airspace?

I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?

What do the pedals on grand pianos do?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

When should a commit not be version tagged?

What are all the squawk codes?

The need of reserving one's ability in job interviews

Where is this triangular-shaped space station from?

Tcolorbox as an item in list environment

Make me a metasequence

Did Amazon pay $0 in taxes last year?

What is better: yes / no radio, or simple checkbox?

What is this waxed root vegetable?

A right or the right?

What type of postprocessing gives the effect of people standing out

Borrowing Characters

How do ISS astronauts "get their stripes"?

What can I substitute for soda pop in a sweet pork recipe?



Django post_save signal called before save done



2019 Community Moderator ElectionDjango's post_save signal behaves weirdly with models using multi-table inheritanceDjango - post_save timing issue - how to implement a signal for action “database save finalized”Is there any difference in the use of the signals post_delete and post_save?Reading a recursive field on post_save of django modelDjango assert post_save signal calledhandle `post_save` signal in celeryDjango - Multiple post_save signals after create despite dispatch_uidDjango-polymorphic pre-save signaldjango : How to use signals?How to check if Django Signal works?










0















When creating an object with a custom save method the post_save signal is called before the database object is really saved to the database.



class Task(models.Model):
date_last_state_change = models.DateTimeField(null=True, blank=True)

def save(self, *args, **kwargs):
print("SAVING TASK")
print(self.date_last_state_change)
super().save(*args, **kwargs)
print("SAVING TASK 2")
print(self.date_last_state_change)


And following signals:



@receiver(post_save, sender=Task)
def create_initial_tag_for_task(instance, created, *args, **kwargs):
if created:
TagMapping.objects.create(content_object=instance, tag=TagMapping.add_and_return_tag("open"))


@receiver(post_save, sender=TagMapping)
def set_timestamp_for_mapped_task(instance, created, *args, **kwargs):
task_type = ContentType.objects.get_for_model(Task)
if instance.tag.is_state and instance.content_type.pk == task_type.pk:
task = Task.objects.get(pk=instance.object_id)
task.date_last_state_change = datetime.now()
task.save()


So when I create a Task I create a generic relation to that object and give it a tag. As soon as I created a state tag I want to set the property of the Task object.



And this is the console output when creating a new Task:



SAVING TASK
None
SAVING TASK
2019-03-05 14:12:25.958946
SAVING TASK 2
2019-03-05 14:12:25.958946
SAVING TASK 2
None


With that the date in the database object is None. I really don't understand why.









share


























    0















    When creating an object with a custom save method the post_save signal is called before the database object is really saved to the database.



    class Task(models.Model):
    date_last_state_change = models.DateTimeField(null=True, blank=True)

    def save(self, *args, **kwargs):
    print("SAVING TASK")
    print(self.date_last_state_change)
    super().save(*args, **kwargs)
    print("SAVING TASK 2")
    print(self.date_last_state_change)


    And following signals:



    @receiver(post_save, sender=Task)
    def create_initial_tag_for_task(instance, created, *args, **kwargs):
    if created:
    TagMapping.objects.create(content_object=instance, tag=TagMapping.add_and_return_tag("open"))


    @receiver(post_save, sender=TagMapping)
    def set_timestamp_for_mapped_task(instance, created, *args, **kwargs):
    task_type = ContentType.objects.get_for_model(Task)
    if instance.tag.is_state and instance.content_type.pk == task_type.pk:
    task = Task.objects.get(pk=instance.object_id)
    task.date_last_state_change = datetime.now()
    task.save()


    So when I create a Task I create a generic relation to that object and give it a tag. As soon as I created a state tag I want to set the property of the Task object.



    And this is the console output when creating a new Task:



    SAVING TASK
    None
    SAVING TASK
    2019-03-05 14:12:25.958946
    SAVING TASK 2
    2019-03-05 14:12:25.958946
    SAVING TASK 2
    None


    With that the date in the database object is None. I really don't understand why.









    share
























      0












      0








      0








      When creating an object with a custom save method the post_save signal is called before the database object is really saved to the database.



      class Task(models.Model):
      date_last_state_change = models.DateTimeField(null=True, blank=True)

      def save(self, *args, **kwargs):
      print("SAVING TASK")
      print(self.date_last_state_change)
      super().save(*args, **kwargs)
      print("SAVING TASK 2")
      print(self.date_last_state_change)


      And following signals:



      @receiver(post_save, sender=Task)
      def create_initial_tag_for_task(instance, created, *args, **kwargs):
      if created:
      TagMapping.objects.create(content_object=instance, tag=TagMapping.add_and_return_tag("open"))


      @receiver(post_save, sender=TagMapping)
      def set_timestamp_for_mapped_task(instance, created, *args, **kwargs):
      task_type = ContentType.objects.get_for_model(Task)
      if instance.tag.is_state and instance.content_type.pk == task_type.pk:
      task = Task.objects.get(pk=instance.object_id)
      task.date_last_state_change = datetime.now()
      task.save()


      So when I create a Task I create a generic relation to that object and give it a tag. As soon as I created a state tag I want to set the property of the Task object.



      And this is the console output when creating a new Task:



      SAVING TASK
      None
      SAVING TASK
      2019-03-05 14:12:25.958946
      SAVING TASK 2
      2019-03-05 14:12:25.958946
      SAVING TASK 2
      None


      With that the date in the database object is None. I really don't understand why.









      share














      When creating an object with a custom save method the post_save signal is called before the database object is really saved to the database.



      class Task(models.Model):
      date_last_state_change = models.DateTimeField(null=True, blank=True)

      def save(self, *args, **kwargs):
      print("SAVING TASK")
      print(self.date_last_state_change)
      super().save(*args, **kwargs)
      print("SAVING TASK 2")
      print(self.date_last_state_change)


      And following signals:



      @receiver(post_save, sender=Task)
      def create_initial_tag_for_task(instance, created, *args, **kwargs):
      if created:
      TagMapping.objects.create(content_object=instance, tag=TagMapping.add_and_return_tag("open"))


      @receiver(post_save, sender=TagMapping)
      def set_timestamp_for_mapped_task(instance, created, *args, **kwargs):
      task_type = ContentType.objects.get_for_model(Task)
      if instance.tag.is_state and instance.content_type.pk == task_type.pk:
      task = Task.objects.get(pk=instance.object_id)
      task.date_last_state_change = datetime.now()
      task.save()


      So when I create a Task I create a generic relation to that object and give it a tag. As soon as I created a state tag I want to set the property of the Task object.



      And this is the console output when creating a new Task:



      SAVING TASK
      None
      SAVING TASK
      2019-03-05 14:12:25.958946
      SAVING TASK 2
      2019-03-05 14:12:25.958946
      SAVING TASK 2
      None


      With that the date in the database object is None. I really don't understand why.







      python django django-models orm





      share












      share










      share



      share










      asked 7 mins ago









      Dominic M.Dominic M.

      79110




      79110






















          0






          active

          oldest

          votes











          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%2f55004040%2fdjango-post-save-signal-called-before-save-done%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          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%2f55004040%2fdjango-post-save-signal-called-before-save-done%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

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved