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

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко