Re: Queryset from 2 models with second model filtered.

2010-12-09 Thread pbzRPA
Typically I simply want to do a left join but have it as an object in
django rather than a sql raw query.

The queryset would look like.

Select
a.code as 'product_code',
a.description as 'product_description',
b.code as 'customer_code',
b.code as 'customer_description',
ifnull(b.code, a.code) as 'code',
ifnull(b.description, b.description) as 'description'
from
products a
left join customerproduct b on b.product_id = a.id where
b.customer_id = 'X'

I know I could create a view in sql and create a model for that but I
am sure there must be a nicer solution.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Queryset from 2 models with second model filtered.

2010-12-09 Thread pbzRPA
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-us...@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.