Thursday, May 1, 2014

select_related reverse?(!)

Okay, I'm a little confused here about using select_ralated...

Can you only run select_related on a table that HAS a foreign key not a table that IS a foreign key?

What if I want to go: 
SELECT * FROM product LEFT JOIN review on product.id = review.product_id LEFT JOIN comments ON product.id = comment.product_id

example models.py

class Product(models.Model):
    name =
    
class Review(models.Model):
    product = models.Foreignkey(Product)
    rating =

class Comments(models.Model):
    product = models.Foreignkey(Product)
    text =
    
In my templates I want to be able to loop through all reviews and comments for each product without hitting the database multiple times...

How can you achieve this?



--
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+unsubscribe@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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5370daf6-58d0-4573-a282-36c4a70a5937%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment