Reputation: 333
I have set up a few models as follows:
Product
Category
Product and Category share a many to many relationship which makes a third table product_categories which holds the product id and category id.
I want to display the list of products by its category.*emphasized text*I Now i have category id but i don't know how to fetch data from this intermediate table of M2M.
So please suggest me some way of doing this.
Thanks.
Edited
i have tried this stuff
my model
class Product():
image = CharField(_("Image"), max_length=100, blank=True, null=True)
style_idea = models.TextField(_("style idea"), blank=True)
categories = models.ManyToManyField("Category", blank=True,
verbose_name=_("Product categories"))
my view
if page.id == 11:
value = Category.objects.all()
value2 = Product.objects.all()
value1 = ProductVariation.objects.all()
return render_to_response('boutique.html',{'page':page,'productvariation':value1,'category':value,'products':value2} , context_instance=RequestContext(request))
my template
{% regroup products by category as products_by_category %}
{% for c in products_by_category %}
{{c}}
{%endfor%}
this c prints all the products
Upvotes: 0
Views: 1100
Reputation: 28637
Normally you would do something like
category = Category.objects.get(pk=10)
products = category.product_set.all() # note that this is a queryset
Upvotes: 1