Bilesanmi Ahmad
Published in

Bilesanmi Ahmad

How to prevent “RecursionError: maximum recursion depth exceeded” when using Django Signals.

Django Logo
class Category(models.Model):    name = models.CharField('Name', max_length=30)    description = models.TextField('Description', blank=True)
class Favourite(models.Model): title = models.CharField('Title', max_length=20) description = models.TextField('Description', blank=True) rank = models.SmallIntegerField('Ranking',) category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='categories')
def save_favorite(sender, instance, **kwargs):
fav = instance
try:
favs = Favourite.objects.filter(rank__lte=fav.rank, category=fav.category)
for f in favs:
f.rank += 1
f.save()
print('ending')
except Favourite.DoesNotExist:
pass
pre_save.connect(save_favorite, sender=Favourite)
RecursionError: maximum recursion depth exceeded while calling a Python object
def save_favorite(sender, instance, **kwargs):
fav = instance
Favourite.objects.filter(rank__lte=fav.rank, category=fav.category).update(rank=F('rank') + 1)
pre_save.connect(save_favorite, sender=Favourite)

--

--

I am Bilesanmi Ahmad. I am a software developer who likes to share his knowledge about software development, Islam and life in general

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store