Hi
I am using django version 1.4.3
I am using two signals in my models.py
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.
my models.py is:

class CartItem(models.Model):
    content_type    = models.ForeignKey(ContentType)
    object_id       = models.PositiveIntegerField()
    content_object  = generic.GenericForeignKey('
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.name

class 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 ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to