I'm facing the problem with the following code:
class Cart(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
items = models.ManyToManyField(CC, blank=True)
total = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
def __str__(self):
return self.user.username
@receiver(pre_save, sender=Cart)
def cart_update_total(sender, instance, *args, **kwargs):
total = Decimal(0.00)
for item in instance.items.all():
total += item.price
instance.total = total
@receiver(user_activated)
def user_created(sender, user, request, **kwargs):
cart, created = Cart.objects.get_or_create(user=user)
So basically I create a Cart
model for user who activates account. But also I'm using pre_save
signal for Cart
where I calculate total price for products in cart. Therefore pre_save
function tries to access fields of object which has not been saved yet. Can you guys advise me how can I modify this logic to get it work correctly?