[Solved]-Avoid recursive save() when using celery to update Django model fields

14👍

Add a keyword argument that tells save not to recurse:

 def save(self, elevation_data=True, *args, **kwargs):
   super(Route, self).save(*args, **kwargs)
   if elevation_data:
     from .tasks import get_elevation_data
     get_elevation_data.delay(self)

And then:

 from celery.decorators import task

 @task()
 def get_elevation_data(route):
     ...
     route.elevation_data = results
     route.save(elevation_data=False)

Leave a comment