Re: Is there any way to apply a constraint on ForeignKey/OneToOneField being attached to any model based on some condition ?

2013-02-08 Thread vijay shanker
fortunately my case was really simple, i had to count number of m2m objects 
attached to my model and storing it as field, and  had to make the decision 
of having a ForeignKey/OneToOne relation based on number of m2m objects 
attached.I was already using m2m_changed signal ( on post_add ), so i 
handled the case there itself
thanks tran.
:)

On Thursday, February 7, 2013 2:33:46 PM UTC+5:30, Huu Da Tran wrote:
>
> You could overwrite the save() method... or if you are populating from a 
> form, overwrite the clean() method.
>
> On Thursday, February 7, 2013 3:23:54 AM UTC-5, vijay shanker wrote:
>>
>> Hi
>> I have these two models Cart and CartItem,
>>
>> 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)
>> applied_rule= 
>> models.OneToOneField(CartRule,null=True,blank=True)
>> 
>>
>> 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)
>> def __str__(self):
>> return self.cart_id
>>
>> the rules to be applied only when there are more than two items in 
>> shoppingcart, i.e i am looking for a way to attach a rule to cart only if 
>> there are more than two items in cart (cartitems m2m field should have more 
>> than two objects in it).
>> How should i go about it ?
>>
>

-- 
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.




Re: Is there any way to apply a constraint on ForeignKey/OneToOneField being attached to any model based on some condition ?

2013-02-07 Thread Huu Da Tran
You could overwrite the save() method... or if you are populating from a 
form, overwrite the clean() method.

On Thursday, February 7, 2013 3:23:54 AM UTC-5, vijay shanker wrote:
>
> Hi
> I have these two models Cart and CartItem,
>
> 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)
> applied_rule= 
> models.OneToOneField(CartRule,null=True,blank=True)
> 
>
> 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)
> def __str__(self):
> return self.cart_id
>
> the rules to be applied only when there are more than two items in 
> shoppingcart, i.e i am looking for a way to attach a rule to cart only if 
> there are more than two items in cart (cartitems m2m field should have more 
> than two objects in it).
> How should i go about it ?
>

-- 
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.




Is there any way to apply a constraint on ForeignKey/OneToOneField being attached to any model based on some condition ?

2013-02-07 Thread vijay shanker
Hi
I have these two models Cart and CartItem,

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)
applied_rule= 
models.OneToOneField(CartRule,null=True,blank=True)


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)
def __str__(self):
return self.cart_id

the rules to be applied only when there are more than two items in 
shoppingcart, i.e i am looking for a way to attach a rule to cart only if 
there are more than two items in cart (cartitems m2m field should have more 
than two objects in it).
How should i go about it ?

-- 
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.