Thursday, December 9, 2010

Queryset from 2 models with second model filtered.

Hi,

I have a tricky one, I don't have an error but rather trying to avoid
many database hits.

I am trying to build a product database which can have different field
values per customer.

So the model would look something like this.

class Customer(models.Model):
name = models.CharField(max_length = 50)

class Product(models.Model):
code = models.CharField(max_length = 32)
description = models.CharField(max_length = 50)

class CustomerProduct(models.Model):
product = models.ForeignKey(Product)
customer = models.ForeignKey(Customer)
code = models.CharField(max_length = 32, null = True, blank =
True)
description = models.CharField(max_length = 50, null = True, blank
= True)

The CustomerProduct will only have an entry if a code or description
is provided for a customer. Now ideally I would like to pull a
queryset for a customer which lists all available products (Product),
but if it finds that a CustomerProduct exists that it returns the
CustomerProduct code and description instead of the Product code and
description.

Even better would be having a queryset filtered by Customer that I
could have as 1 object and call the field values like x.product.code
and x.customerproduct.code.

Just looking for some suggestions.

Thanks

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate