我建立了以下几个模型:
产品
范畴
产品和类别共享多到多的关系,这使得包含产品id和类别id的第三个表product_categories。
我想按类别显示产品列表。*强调文本*我现在有类别id,但我不知道如何从M2M的中间表中获取数据。
所以请给我一些建议。
谢谢。
编辑过的
我试过这种东西
我的模型
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"))我的视图
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))我的模板
{% regroup products by category as products_by_category %}
{% for c in products_by_category %}
{{c}}
{%endfor%} 这个c打印所有的产品
发布于 2012-12-10 09:46:06
通常你会做这样的事
category = Category.objects.get(pk=10)
products = category.product_set.all() # note that this is a querysethttps://stackoverflow.com/questions/13797952
复制相似问题