one
is m2m_changed, so i can have a counter field (numcounter) of number of
m2m fields attached, and another is post_save so i can decide whether
to have a OneToOneField (cartrule, is to be applied only if there are
more than 2 fields) or not.
content_type','object_id')
quantity = models.PositiveIntegerField(default=0)
is_abandoned = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return
self.content_object.nameclass CartRule(models.Model):
##some field
pass
class Cart(models.Model):
cart_id = models.CharField(max_length=50, null=False)
customer = models.ForeignKey(Customer,null=True,blank=True)
cartitems = models.ManyToManyField(CartItem,null=True)
created_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
cartrule = models.OneToOneField(crapclass,null=True,blank=True)
num_cartitem = models.IntegerField()
def __str__(self):
return self.cart_id
@receiver(post_save, sender=Cart)
def apply_condition(sender,instance,created,raw,using,*args,**kwargs):
# i want to decide here if num_cartitem is greater than 2 its ok to have a cartrule
pass
@receiver(m2m_changed)
def save_cartitem_counter(sender, instance, signal,*args, **kwargs):
if kwargs['action'] == 'post_add':
instance.num_cartitem = instance.cartitems.all().count()
instance.save()
the
issue is apply_condition gets called twice, with similar value of args,
first with older value of m2m (cartitem) field in Cart, the other time with the values i intended to save
I looked into older post but still could not figure out the whys.How should i go about this ?