On Thu, Jul 21, 2011 at 10:50 PM, nixlists <nixmli...@gmail.com> wrote:

> On Thu, Jul 21, 2011 at 2:17 PM, Jani Tiainen <rede...@gmail.com> wrote:
> > Hi,
> > So you want to tie Contract with Product(s) with rebate_pct? You then
> need
> > custom intermediary m2m table say "ContractProduct"
> > <see
> https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manytomany
> >
> > for more. So in the end your models would probably look a alike
> following:
> > class Contract():
> >     contract_id = models.lIntegerField(...)
> >     products = models.ManyToManyField(Product, through='ContractProduct')
> > class ContractProduct():
> >     contract = models.ForeignKey(Contract)
> >     product = models.ForeignKey(Product)
> >     rebate_pct = models.DecimalField(max_digits=4, decimal_places=2)
> > So now you can link Contract with multiple Products adding custom
> rebate_pct
> > value to each link.
> > And what comes to rebate value, I think you want to keep value with
> product
> > linkage for two reasons:
> > 1) It's a single scalar value
> > 2) It's probably something that should not be changed ever after saving.
> > hth,
>
> Thanks for your response, I think this will work. How would I write a
> query to find the rebate_pct per contract per product?
>
>
ContractProduct.objects.all()

Following might work also (not sure, but is easy to test in shell for
example):

for c in Contract.objects.all():
    for cp in c.contractproduct_set.all():
        print c, cp.product, cp.rebate_pct

-- 

Jani Tiainen

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

Reply via email to