Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Queryset from 2 models with second model filtered. [RSS Feed]

#1 Dec. 9, 2010 20:03:44

p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 9, 2010 20:27:00

p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Queryset from 2 models with second model filtered.


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Queryset from 2 models with second model filtered. [RSS Feed]

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support