I’m struggling to try and figure out why a post_save function is returning:
Exception Type: RuntimeError
Exception Value: maximum recursion depth exceeded
Here is my code:
#post save functions of the order
def orderPs(sender, instance=False, **kwargs):
if instance.reference is None:
instance.reference = str(friendly_id.encode(instance.id))
#now update the amounts from the order items
total = 0
tax = 0
#oi = OrderItem.objects.filter(order=instance)
#for i in oi.all():
# total += i.total
# tax += i.total_tax
instance.total = total
instance.tax = tax
instance.save()
#connect signal
post_save.connect(orderPs, sender=Order)
I’ve commented out the order items code for now.
instance.total and instance.tax are model decimal fields.
It seems like the post_save function is in an endless loop, not sure why as I’ve used the same format for all of my post_save functions.
Any ideas?
You are calling
instance.save()in your post save signal thus triggering it recursively.All your edited fields in this signal receiver are quite simply derived from other values already stored in the database thus creating redundancies. This is usually not a good idea. Write properties or cached properties instead: