title: Python Celery: Update django model after state change
body: <p>I managed to find 2 similar topics with discussion about this issue, but unfortunately I couldn't get what is the best solution from it:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/47737175/update-django-model-field-based-on-celery-task-status">Update Django Model Field Based On Celery Task Status</a></li>
  <li><a href="https://stackoverflow.com/questions/47737175/update-django-model-field-based-on-celery-task-status">Update Django Model Field Based On Celery Task Status</a></li>
  </ol>
  
  <p>I use Django &amp; Celery (+redis as Message broker) and I would like to update django model when celery task status changes(from pending -> success, pending -> failure) etc.</p>
  
  <p>My code:</p>
  
  <pre><code>import time
  
  from celery import shared_task
  
  @shared_task(name="run_simulation")
  def run_simulation(simulation_id: str):
      t1_start = time.perf_counter()
      doSomeWork() # we may change this to sleep for instance
      t1_end = time.perf_counter()
      return{'process_time': t1_end - t1_start}
  </code></pre>
  
  <p>and the particular view from which I am calling the task:</p>
  
  <pre><code>def run_simulation(request):
      form = SimulationForm(request.POST)
      if form.is_valid():
          new_simulation = form.save()
          new_simulation.save()
          task_id = tasks.run_simulation.delay(new_simulation.id)
  </code></pre>
  
  <p>The question is, what is the preferred way to update django model state of Simulation when status of task has been changed?</p>
  
  <p>In the docs I found handlers that are using methods <code>on_failure</code>, <code>on_success</code> etc. <a href="http://docs.celeryproject.org/en/latest/userguide/tasks.html#handlers" rel="nofollow noreferrer">http://docs.celeryproject.org/en/latest/userguide/tasks.html#handlers</a></p>